Hello there!
I'm writting here because I'm trying to find some help with remake a script. I found two versions of PlayerController. First is for Keyboard, more complicated, have more options, the second one is for touch screen, very simple one. I'm not good with programming, I'm a 3D graphic designer but I need this script for my little project. I'm working in Unity 4.6 but I can't find any help with my problem in tutorials.
The first script (for keyboard):
PlayerController
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(PlayerPhysics))]
public class PlayerController : Entity {
public float gravity = 20;
public float walkSpeed = 8;
public float runSpeed = 12;
public float acceleration = 30;
public float jumpHeight = 12;
public float slideDeceleration = 10;
private float initiateSlideThreshold = 9;
private float animationSpeed;
private float currentSpeed;
private float targetSpeed;
private Vector2 amountToMove;
private float moveDirX;
private bool jumping;
private bool sliding;
private bool wallHolding;
private bool stopSliding;
private PlayerPhysics playerPhysics;
private Animator animator;
private GameManager manager;
void Start () {
playerPhysics = GetComponent<PlayerPhysics>();
animator = GetComponent<Animator>();
manager = Camera.main.GetComponent<GameManager>();
animator.SetLayerWeight(1,1);
}
void Update () {
if (playerPhysics.movementStopped) {
targetSpeed = 0;
currentSpeed = 0;
}
if (playerPhysics.grounded) {
amountToMove.y = 0;
if (wallHolding) {
wallHolding = false;
animator.SetBool("Wall Hold", false);
}
if (jumping) {
jumping = false;
animator.SetBool("Jumping",false);
}
if (sliding) {
if (Mathf.Abs(currentSpeed) < .25f || stopSliding) {
stopSliding = false;
sliding = false;
animator.SetBool("Sliding",false);
playerPhysics.ResetCollider();
}
}
if (Input.GetButtonDown("Slide")) {
if (Mathf.Abs(currentSpeed) > initiateSlideThreshold) {
sliding = true;
animator.SetBool("Sliding",true);
targetSpeed = 0;
playerPhysics.SetCollider(new Vector3(10.3f,1.5f,3), new Vector3(.35f,.75f,0));
}
}
}
else {
if (!wallHolding) {
if (playerPhysics.canWallHold) {
wallHolding = true;
animator.SetBool("Wall Hold", true);
}
}
}
if (Input.GetButtonDown("Jump")) {
if (sliding) {
stopSliding = true;
}
else if (playerPhysics.grounded || wallHolding) {
amountToMove.y = jumpHeight;
jumping = true;
animator.SetBool("Jumping",true);
if (wallHolding) {
wallHolding = false;
animator.SetBool("Wall Hold", false);
}
}
}
animationSpeed = IncrementTowards(animationSpeed,Mathf.Abs(targetSpeed),acceleration);
animator.SetFloat("Speed",animationSpeed);
moveDirX = Input.GetAxisRaw("Horizontal");
if (!sliding) {
float speed = (Input.GetButton("Run"))?runSpeed:walkSpeed;
targetSpeed = moveDirX * speed;
currentSpeed = IncrementTowards(currentSpeed, targetSpeed,acceleration);
if (moveDirX !=0 && !wallHolding) {
transform.eulerAngles = (moveDirX>0)?Vector3.up * 180:Vector3.zero;
}
}
else {
currentSpeed = IncrementTowards(currentSpeed, targetSpeed,slideDeceleration);
}
amountToMove.x = currentSpeed;
if (wallHolding) {
amountToMove.x = 0;
if (Input.GetAxisRaw("Vertical") != -1) {
amountToMove.y = 0;
}
}
amountToMove.y -= gravity * Time.deltaTime;
playerPhysics.Move(amountToMove * Time.deltaTime, moveDirX);
}
void OnTriggerEnter(Collider c) {
if (c.tag == "Checkpoint") {
manager.SetCheckpoint(c.transform.position);
}
if (c.tag == "Finish") {
manager.EndLevel();
}
}
private float IncrementTowards(float n, float target, float a) {
if (n == target) {
return n;
}
else {
float dir = Mathf.Sign(target - n);
n += a * Time.deltaTime * dir;
return (dir == Mathf.Sign(target-n))? n: target;
}
}
}
And the second one (for touch screen). Well.. This one is made in 4 parts so I'm more confused with it.
PlayerMovement
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour
{
public float speed = 500f, jumpHeight = 500f;
Transform myTrans;
Rigidbody2D myBody;
Vector2 movement;
bool isGrounded = true;
void Start ()
{
myTrans = this.transform;
myBody = this.rigidbody2D;
}
void Update ()
{
isGrounded = Physics2D.Linecast(myTrans.position,
GameObject.Find(this.name+"/tag_ground").transform.position,
LayerMask.NameToLayer("Player"));
Debug.DrawLine (myTrans.position,
GameObject.Find(this.name+"/tag_ground").transform.position,
Color.red);
#if !UNITY_ANDROID && !UNITY_IPHONE && !UNITY_BLACKBERRY && !UNITY_WINRT
Move (Input.GetAxisRaw("Horizontal"));
if (Input.GetButtonDown ("Jump"))
Jump ();
#endif
}
public void Move(float horizontal_input)
{
movement = myBody.velocity;
if(isGrounded)
movement.x = horizontal_input * speed * Time.deltaTime;
myBody.velocity = movement;
}
public void Jump()
{
if(isGrounded)
myBody.velocity += jumpHeight * Vector2.up * Time.deltaTime;
}
}
MoveButton
using UnityEngine;
using System.Collections;
public class MoveButton : TouchLogicV2
{
[Range(-1,1)]
public int moveDir = 1;
PlayerMovement player;
void Start()
{
player = FindObjectOfType<PlayerMovement>();
}
public override void OnTouchBegan()
{
touch2Watch = currTouch;
}
public override void OnTouchMoved()
{
player.Move (moveDir);
}
public override void OnTouchStayed()
{
player.Move (moveDir);
}
public override void OnTouchEndedAnywhere()
{
if(currTouch == touch2Watch)
player.Move(0);
}
}
JumpButton
using UnityEngine;
using System.Collections;
public class JumpButton : TouchLogicV2
{
PlayerMovement player;
void Start()
{
player = FindObjectOfType<PlayerMovement>();
}
public override void OnTouchBegan()
{
player.Jump ();
}
}
TouchLogicV2
using UnityEngine;
using System.Collections;
public class TouchLogicV2 : MonoBehaviour
{
public static int currTouch = 0;
[HideInInspector]
public int touch2Watch = 64;
public virtual void Update()
{
if(Input.touches.Length <= 0)
{
OnNoTouches();
}
else
{
for(int i = 0; i < Input.touchCount; i++)
{
currTouch = i;
if(this.guiTexture != null && (this.guiTexture.HitTest(Input.GetTouch(i).position)))
{
if(Input.GetTouch(i).phase == TouchPhase.Began)
{
OnTouchBegan();
touch2Watch = currTouch;
}
if(Input.GetTouch(i).phase == TouchPhase.Ended)
{
OnTouchEnded();
}
if(Input.GetTouch(i).phase == TouchPhase.Moved)
{
OnTouchMoved();
}
if(Input.GetTouch(i).phase == TouchPhase.Stationary)
{
OnTouchStayed();
}
}
switch(Input.GetTouch(i).phase)
{
case TouchPhase.Began:
OnTouchBeganAnywhere();
break;
case TouchPhase.Ended:
OnTouchEndedAnywhere();
break;
case TouchPhase.Moved:
OnTouchMovedAnywhere();
break;
case TouchPhase.Stationary:
OnTouchStayedAnywhere();
break;
}
}
}
}
public virtual void OnNoTouches(){}
public virtual void OnTouchBegan(){print (name + " is not using OnTouchBegan");}
public virtual void OnTouchEnded(){}
public virtual void OnTouchMoved(){}
public virtual void OnTouchStayed(){}
public virtual void OnTouchBeganAnywhere(){}
public virtual void OnTouchEndedAnywhere(){}
public virtual void OnTouchMovedAnywhere(){}
public virtual void OnTouchStayedAnywhere(){}
}
I'm sorry for long post, but I really need it badly. I know what these things means, I understand the code, but I'm just not able to remake it myself. I need the first code (keyboard) to remake for touch screen based on the rest of presented scripts (touch). Well... If it's possible of course, because I could better understand tutorials for it (I've asked authors for help but there is no answer).
Best regards,
Dr Comet