hi everyone. i am currently on my university project to build an endless run game 3D. So basically the character will be moving forward on its own. Ive got the moving code right but the thing is that when i press space key to jump, the character will jump without moving forward. For example, the character will run forward, jump and then continue running forward. What i really want it that, when i press the jump key, the character will jump together with moving forward on the air. Ive been searching for a week to get the right code and method but nothing seems to work. Currently i only left 1 and a half week to get this jumping thing done.
Here is the code, sorry if the code is a bit mess up:
pragma strict
// variables declaration
var speed : float = 2.0;
var defaultSpeed = 2.0;
var increaseAmount : int = 1;
var gravity : float = 20.0;
var jumpSpeed : float;
var jumpForce : float = 1000;
var turnSpeed : float = 150.0;
var jumpHeight : float;
var moveForward:boolean = false;
private var moveDirection : Vector3 = Vector3.zero;
private var vertVel: float = 0;
InvokeRepeating(“IncreaseSpeed”, 4, 4);
function IncreaseSpeed ()
{
defaultSpeed += increaseAmount;
}
function Start ()
{
// by default, set all animation to loop
animation.wrapMode = WrapMode.Loop;
//separate animation into different layers
animation["run"].layer = -1;
animation["idle"].layer = -1;
//animation["jump"].layer = -1;
animation.SyncLayer(-1);
animation["die"].layer = 9;
animation["die"].wrapMode = WrapMode.Once;
animation["jump"].layer = 9;
animation["jump"].wrapMode = WrapMode.Once;
animation["dodge"].layer = 9;
animation["dodge"].wrapMode = WrapMode.Once;
animation.SyncLayer(9);
animation.Stop();
animation.Play("run");
}
function Update()
{
var mainChar:GameObject = GameObject.Find(“pandanman”);
//var turn: float = Input.GetAxis("Horizontal");
//transform.(0, turn * 90 * Time.deltaTime , 0);
var controller : CharacterController = GetComponent(CharacterController);
var upForce=200;
var fwdForce=200;
if (controller.isGrounded)
{
//We are grounded, so recalculate
//move direction directly from axes
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
Input.GetAxis("Vertical"));
//moveDirection = transform.forward * Input.GetAxis("Vertical") * speed*6;
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= 12;
//Moving endless forward
controller.Move(transform.forward * defaultSpeed * Time.deltaTime);
if (Input.GetKeyDown(KeyCode.Space))
{
print("Character should jump");
animation.CrossFade("jump");
//controller.Move(Vector3.up *3);
moveDirection.y = jumpSpeed;
}
if (Input.GetKeyDown(KeyCode.LeftAlt))
{
print("Character should dodge");
animation.Play("dodge");
}
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
controller.Move(moveDirection * Time.deltaTime);
}
@script RequireComponent(CharacterController)
Would be very happy and appreciate for any help and opinion. thanks!