I WILL GIVE YOU 3 DOLLARS to answer this constantforce question

I’ll paypal you 3 dollars(I’m poor as fuck) if you can fix my script so that it works the way I want it to work, it’s driving me crazy! For some reason the later if statement cancels out the first one. Here’s my code:

private var rotateSpeed : float = 0.0;
private var flipSpeed	: float = 0.0;
var speedness : float = 20.0;

function StartSpinning ()
	{	
		rigidbody.constantForce.relativeTorque = Vector3 (0, rotateSpeed * Time.deltaTime, 0);
	}

function StartFlipping ()
	{	
		rigidbody.constantForce.relativeTorque = Vector3 (0, 0, flipSpeed * Time.deltaTime);
	}
	
	
function FixedUpdate ()
	{
				if (Input.GetKey("left"))
		{
			rotateSpeed = speedness;
			StartSpinning();	
		}	
		if (!Input.GetKey("left"))
		{
			rotateSpeed = 0;
			StartSpinning();
		}	
			if (Input.GetKey("up"))
		{
			flipSpeed = -speedness;
			StartFlipping();	
		}	
		if (!Input.GetKey("up"))
		{
			flipSpeed = 0;
			StartFlipping();
		}
	
	}

StartSpinning and StartFlipping are zeroing all axes but the one they set, so the later always cancels the former. You should alter only the axis used in each function. Additionally, you should not multiply the speeds by Time.deltaTime (it may even work, since in FixedUpdate it’s usually fixed to 0.020):

	function StartSpinning ()
	{	
		rigidbody.constantForce.relativeTorque.y = rotateSpeed * 0.02;
	}

	function StartFlipping ()
	{	
		rigidbody.constantForce.relativeTorque.z = flipSpeed * 0.02;
	}

If this answer solve your problem, please click the “check” button below the voting thumbs to mark it as accepted - and save your 3 bucks by now: if you get rich someday I’ll send a Paypal money request to you with the interest rates added.

Try using Input.GetAxis(“Horizontal”), this does what you want without having to go through each individual key… Here’s the scriptreference.

Try replacing your second and fourth if statements, the ones with if(!Input…), with else statements. Also, is the code running or are you getting compile errors.

Try replacing your second and fourth if statements, the ones with if(!Input…), with else statements. Also, is the code running or are you getting compile errors.

Or if you want to use Input.GetAxis, try this:

private var rotateSpeed : float = 10.0; 
private var flipSpeed : float = 10.0; 

function FixedUpdate () { 
	var torqueY = Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime;
	var torqueZ = Input.GetAxis("Vertical") * flipSpeed * Time.deltaTime;
	rigidbody.constantForce.relativeTorque = Vector3 (0, torqueY, torqueZ); 
	
}

I would recommend the GetAxis approach because it has much cleaner code.

var rotateSpeed : float = 0.0;
var flipSpeed : float = 0.0;
var speedness : float = 20.0;

function Update (){
	if (Input.GetKey("left")){
		rotateSpeed = speedness;	
	}	
	else{
		rotateSpeed = 0;
	}
		
	if (Input.GetKey("up")){
		flipSpeed = -speedness;	
	}	
	else{
		flipSpeed = 0;
	}
	
	rigidbody.constantForce.relativeTorque = Vector3 (0, rotateSpeed, flipSpeed);	
}

try this, it worked for me for what I was guessing you were trying to make it do however as you didn’t say exactly the effect you were trying to achieve I might have been doing the wrong thing

Scribe

P.S
I don’t want your money :slight_smile: