Swimmin'

This is probably the most random problem I’ve ever had, but here goes nothin’. I’m making a game about Sgid (don’t ask) and I wrote a script to make him float around. The script looks like this:

function Update () {
	if (Input.GetButton("right")) {
		transform.Rotate (0, 8, 0);
	}
	if (Input.GetButton("left")) {
		transform.Rotate (0, -8, 0);
	}
	if (Input.GetButton("up")) {
		rigidbody.AddRelativeForce (Vector3.forward * 15);
	}
	if (Input.GetButton("down")) {
		rigidbody.AddRelativeForce (Vector3.back * 15);
	}
	if (Input.GetButton("FloatUp")) {
		rigidbody.AddRelativeForce (Vector3.up * 15);
	}
	if (Input.GetButton("FloatDown")) {
		rigidbody.AddRelativeForce (Vector3.down * 15);
	}
}

But when I press the button for FloatUp he floats up while rotating counter-clockwise. Why is he rotating? :frowning:

Maybe some of the keyboard mapping are mapped to the same key.

Just some suggestions for the script. It is a lot easier, faster and works with joysticks to use axes instead of if’s.

 if (Input.GetButton("right")) { 
      transform.Rotate (0, 8, 0); 
   } 
   if (Input.GetButton("left")) { 
      transform.Rotate (0, -8, 0); 
   }

becomes this:

 transform.Rotate (0, Input.GetAxis("Horizontal") * 8, 0);

secondly you want to make it frame rate independent by multiplying with delta time:

 transform.Rotate (0, Input.GetAxis("Horizontal") * 200 * Time.deltaTime, 0);

This makes it rotate exactly 200 degrees per second, independent of frame rate.

You can apply the same GetAxis instead of if (button ) technique to the addforce calls.

They definately aren’t mapped to the same button. Completely seperate. :confused: