- Home /
Question by
RainaTaleth · Aug 30, 2014 at 03:14 PM ·
c#androidioscharactercontroller
Jumping while moving problem (CharacterController C#)
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<CharacterController>();
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.
screenshot 2014-08-30 16.33.25.png
(103.2 kB)
Comment
I highly recommend using the best debugger in the world: print statements.
Gotta be more descriptive. Dunno what "jiggers back" means. Back in a direction opposite the current movement? Also I see you're adding gravity and multiplying by deltaTime when not grounded, but you're also doing this on lines 54 and 55. Doubt it's responsible for your issue, but it looks accidental.