![alt text][1]
[1]: /storage/temp/31767-screenshot+2014-08-30+16.33.25.png
Here is the setup for my game. 2.5D platformer for mobile. The problem I am having is that when I am moving and trying to jump the character jiggers back before jumping forward and I can't figure out why. Here is the code:
using UnityEngine;
using System.Collections;
public class CharacterMovement : MonoBehaviour {
public TouchControll moveFWD;
public TouchControll moveBWD;
public TouchControll jumpUP;
private float speed = 5.0f;
private float jumpSpeed = 18.0f;
private bool canJump = true;
private Vector3 velocity;
private Vector3 movement;
private CharacterController character;
private Transform thisTransform;
private bool jump = false;
// Use this for initialization
void Start () {
thisTransform = transform;
character = gameObject.GetComponent();
GameObject spawn = GameObject.Find( "Spawn" );
if ( spawn ) thisTransform.position = spawn.transform.position;
}
// Update is called once per frame
void Update () {
movement = Vector3.zero;
if ( moveFWD.fingerDown ) movement = Vector3.right * speed;
else if ( moveBWD.fingerDown ) movement = Vector3.right * -speed;
if(character.isGrounded){
jump = false;
if(!jumpUP.fingerDown) canJump = true;
if(canJump && jumpUP.fingerDown) {
jump = true;
canJump = false;
}
if ( jump ) {
velocity = character.velocity;
velocity.y = jumpSpeed;
}
} else {
velocity.y += Physics.gravity.y * Time.deltaTime;
//movement.x *= 0.5f;
}
movement += velocity;
movement += Physics.gravity;
movement *= Time.deltaTime;
character.Move( movement );
if ( character.isGrounded ) velocity = Vector3.zero;
}
}
I can't understand what is wrong with it. Thanks in advance.
↧