- Home /
This question was
closed Nov 21, 2016 at 07:10 PM by
Midane53 for the following reason:
The question is answered, right answer was accepted
Question by
Midane53 · Nov 21, 2016 at 07:21 AM ·
transformvector3controllerjumping object
How to keep going forward while jumping ?
Hello, i'm trying so hard to make my character ( a cube ) keep going forward while jumping, but I just can't find a solution. I tried to put my even out of the .isGround if condition, but still doesn't work. Here is my code: ( sorry for the really bad organisation, i'm just trying to get part of codes from here and here, i'm learning from scratch ) using UnityEngine; using System.Collections;
public class CubeMove : MonoBehaviour {
public float speed = 7.0f;
public float jumpSpeed = 6.0f;
public float gravity = 5.0f;
private Vector3 moveDirection = Vector3.zero;
private CharacterController controller;
// Use this for initialization
void Start()
{
Cursor.visible = false;
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
if (controller.isGrounded) {
if (Input.GetKey (KeyCode.Z)) {
transform.Translate (Vector3.forward * 11 * Time.deltaTime);
}
if (Input.GetKey (KeyCode.S)) {
transform.Translate (Vector3.back * 11 * Time.deltaTime);
}
if (Input.GetKey (KeyCode.Q)) {
transform.Rotate (-Vector3.up * 160 * Time.deltaTime);
}
if (Input.GetKey (KeyCode.D)) {
transform.Rotate (Vector3.up * 160 * Time.deltaTime);
}
} else {}
if (Input.GetButton ("Jump")) {
moveDirection = Vector3.up * jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}
Comment
Best Answer
Answer by Dibbie · Nov 21, 2016 at 08:37 AM
Instead of =, use +=
So, your jump line could look like: moveDirection += Vector3.up * jumpSpeed;