transform.Rotate rotates too quickly... can't slow it down.

I’m modifying a controller script from a book. My objective is to use the arrow keys to drive forward/backward and steer/rotate left/right.

The left/right keys rotate properly, but it all occurs too fast. Note the rotateSpeed var, set to 4.0 (which provides for far-too-fast rotation). When I change this to lower values, the left/right rotation becomes a left/right sliding motion instead. I can’t figure out what I’m doing wrong. Can anyone indicate what I can do with this script to control the rate of left/right rotation? Thank you!

var controller:CharacterController;
controller = GetComponent(CharacterController);

var rollSpeed = 6.0;
var gravity = 20.0;
var rotateSpeed = 4.0;
var isControllable:boolean = true;

private var moveDirection = Vector3.zero;
private var grounded:boolean = false;
private var moveHorz = 0.0;
private var rotateDirection = Vector3.zero;

function FixedUpdate() {
	if (!isControllable) {
		Input.ResetInputAxes();
	} else {
		if (grounded) {
			moveDirection = new Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical"));
			moveDirection = transform.TransformDirection(moveDirection);
			moveDirection *= rollSpeed;
			
			moveHorz = Input.GetAxis("Horizontal");
			if (moveHorz > 0) {
				rotateDirection = new Vector3(0,1,0);
			} else if (moveHorz < 0) {
				rotateDirection = new Vector3(0,-1,0);
			} else {
				rotateDirection = new Vector3(0,0,0);
			}
		}
		
		moveDirection.y -= gravity * Time.deltaTime;
		
		var flags = controller.Move(moveDirection * Time.deltaTime);
		controller.transform.Rotate(rotateDirection * Time.deltaTime, rotateSpeed);
		grounded = ((flags & CollisionFlags.CollidedBelow) != 0);
		
	}
}

1 Answer

1

controller.transform.Rotate(rotateDirection * Time.deltaTime, rotateSpeed)

should be

controller.transform.Rotate( rotateDirection * Time.deltaTime * rotateSpeed );

you can also forget the part with rotate direction by simply using the input, which is between -1 for full left and 1 for full right, and multiplying it with the rotatespeed.

controller.transform.Rotate( Vector3.up * Input.GetAxis( "Horizontal" ) * rotateSpeed * Time.deltaTime );

with this you can remove the entire moveHorz part. Also

moveDirection.y -= gravity * Time.deltaTime;
var flags = controller.Move(moveDirection * Time.deltaTime);

this means your gravity is not -9.81 * Time.deltaTime but 9.81 * Time.deltaTime * Time.deltaTime. Remove the last part from the first line.