Variable + .Translate problem

I only have a little bit of code experience from before, so my code is far from good and even further away from perfect… Don’t expect anything more than terrifying pile of letters!

But anyhows, I ran in to a problem with transform.Translate and feeding it with changing variable (I have no idea what is causing the problem).

var player_currentSpeed = 0; //currentSpeed used with .Translate
var player_targetSpeed = 15; //later on will be properly controlled
var player_acceleration = 0.1; //how much the ship is accelerated per cycle
var player_accelerationDelay = 0.25; //delay in acceleration cycle
var player_accelerationNext = 0; //this + Time.time will update 'next cycle'

function Update () 
{
	//propel ship forward via transform.Translate on X.0.0
	transform.Translate(player_currentSpeed * Time.deltaTime,0,0);
	

	// check if current speed is lower than target speed, and that enough time (acceleration delay) has passed, if so, increase speed. else do nothing
	if(player_currentSpeed > player_targetSpeed  Time.time < player_accelerationNext)
		{
			//determine accelerated speed in cycle
			player_currentSpeed = player_currentSpeed + player_acceleration;
			//determine time of next acceleration cycle
			player_accelerationNext = Time.time + player_accelerationDelay;		
		}
}

if I remove the whole ‘increase speed’ part (where Time.time is checked against the next time to do increase) and change player_currentSpeed to XX then the script works. But the increasing itself doesn’t and I have no idea why. Of course I could do another script that would run in the background and occasionally tell this script that the player_currentSpeed has changed, but I’d rather have this script perform all the forward moving of the ship… and this dead end is above my skills :<

I don’t wish a code rewrite, I want to do it myself as I get better, but I want to fix this one so I can start making new scripts to improve my skills… you see… I want to implement all features first as very rought script, then as I progress, I’ll rewrite them better and better till they shine like a rising star :slight_smile:

Cheers to Community and Unity!

Science done.

I changed all but player_targetSpeed to private var and this one to public var ( I want to send target speed information from outside? )

if(player_currentSpeed < player_targetSpeed  Time.time > player_accelerationNext)

is now like this, which works as I tested it by adding transform.Rotate (0.2, 0, 0); right under it. The acceleration delay / next works, but the actual speed increase doesn’t. The speed won’t increase :frowning:

player_currentSpeed = player_currentSpeed + player_acceleration;

does nothing :frowning:

and fixed!

Friend of mine figured out, in exactly 35 minutes, that the script doesn’t understand the speed value is float and not int. Solved by making speed as 0.0 in beginning :slight_smile: