Hi guys i am like making a sort of 3D platform game and i want to give my “Gamer” (which is the moving part) a constant speed so you only have to jump, my question is if there’s a sort of script which lets my gamer advance with a constant speed.
What’s your character? A CharacterController? A Rigidbody? You must use one of them to have automatic collision detection - preferably the CharacterController, since Rigidbodies may react physically to collisions, spinnig or changing direction.
Add a CharacterController to your character and use a script like this to control it (player script):
var speed: float = 4.0; // move speed
var jumpSpeed: float = 8.0; // initial jump speed
var turnSpeed: float = 90; // turn 90 degrees per second
var gravity: float = 9.8;
private var cc: CharacterController;
private var vSpeed: float;
function Update(){
// rotates the character according to the horizontal axis (A-D):
transform.Rotate(0, Input.GetAxis("Horizontal")*turnSpeed*Time.deltaTime, 0);
var moveDir = transform.forward * speed; // calculate the horizontal speed
if (!cc) cc = GetComponent(CharacterController); // get the CharacterController
if (cc.isGrounded){ // when grounded...
vSpeed = 0.0; // vSpeed is zero...
if (Input.GetButtonDown("Jump")){ // unless the character jumps
vSpeed = jumpSpeed;
}
}
vSpeed -= gravity*Time.deltaTime; // apply a physically correct gravity
moveDir.y = vSpeed; // add the vertical speed
cc.Move(moveDir*Time.deltaTime); // and finally move the character
}
var speed : float = 0.1;
var maxHeight : float;
var minHeight : float;
function FixedUpdate(){
//Move Platform Down
if (transform.position.y > maxHeight)
transform.position.y -= speed;
//Move Platform Up
else if (transform.position.y < minHeight)
transform.positioin.y +=speed;
}