Hey, so i’m pretty new to Unity and even newer to programming. I’ve managed to get my character moving roughly how I want him to. However, I’m having problems with his movement being exactly the same every time I run my game.
My character moves along the x axis at a constant speed, when he hits boosters his speed increases then gradually decreases back down to his default speed ( kinda like getting a mushroom power up in mario kart). The main issue I’m having is that the rate at which his speed decreases seems to vary every time I run the game.
I had a look through the forums and the scripting reference and i reckon my problem has something to do with using Time.time or maybe Time.fixedTime, but I’m not quite sure how to implement it into my script.
My script is pretty basic, I originally had it so the boosters gradually increased the players speed by using Mathf.SmoothDamp to calculate their values but found that the speed became even more unpredictable using that method. Any help would be much appreciated!
@script RequireComponent( CharacterController )
private var moveSpeed: float;
var defaultSpeed: float = 3.0;
private var acelx: float = 5.0;
private var smoothTime: float = 1.2;
private var booster : int[] = new int[2];
booster[0] = 25;
booster[1] = 100;
function Start()
{
character = GetComponent( CharacterController );
}
function Update()
{
// SMOOTHS MOVESPEED
moveSpeed = Mathf.SmoothDamp(moveSpeed, defaultSpeed, acelx, smoothTime);
// MOVES THE PLAYER ALONG THE X AXIS
transform.Translate(moveSpeed * Time.deltaTime, 0, 0);
}
// CHECK FOR COLLITIONS ENTER TO ADD SPEED
function OnTriggerEnter(col : Collider)
{
switch ( col.gameObject.name )
{
case "BoostCollider1":
moveSpeed = moveSpeed + 1;
break;
case "BoostCollider2":
moveSpeed = moveSpeed + 8;
break;
case "SpeedTrap":
print(moveSpeed);
break;
}
}