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);
}
}