First of all, I am fairly new to javascript and scripting an general. I say that because this is probably an easy fix, it’s just going over my head.
So pretty much what’s happening is my player keeps rotating after I let go of my rotation key. I actually dug this script up from a long time ago, and it’s weird because when I used it back then it didn’t have this problem and worked perfectly.
All I want is the player to stop rotating when i’m not holding the key down.
So here’s the script:
#pragma strict
@script RequireComponent(CharacterController)
@script RequireComponent(Rigidbody)
public var movementSpeed = 10.0f;
public var rotateSpeed = 10.0f;
public var jumpSpeed = 10.0f;
public var gravity = 20;
private var moveDirection : Vector3 = Vector3.zero;
private var limitDiagonalSpeed = true;
private var antiBunnyHopFactor = 1.0f;
private var jumpTimer : float;
function Start ()
{
jumpTimer = antiBunnyHopFactor;
}
function Update ()
{
var controller : CharacterController = GetComponent(CharacterController);
var inputStrafe = Input.GetAxis("Strafe"); //Originally Horizontal #1
var inputX = Input.GetAxis("Rotate"); //Originally Horizontal #2
var inputY = Input.GetAxis("Vertical");
var inputModifyFactor = (inputStrafe != 0.0 && inputY != 0.0 && limitDiagonalSpeed)? .7071 : 1.0;
if(controller.isGrounded)
{
moveDirection = Vector3(inputStrafe * inputModifyFactor, 0, inputY * inputModifyFactor);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= movementSpeed;
transform.Rotate(0, inputX * rotateSpeed, 0);
//Anti-BunnyHop - Holding space will only allow jumping once; You must hit space again to jump
if(!Input.GetButton("Jump"))
{
jumpTimer++;
}
else if(jumpTimer >= antiBunnyHopFactor)
{
moveDirection.y = jumpSpeed;
jumpTimer = 0.0;
}
}
else //Otherwise, only allow rotation in mid-air
{
transform.Rotate(0, inputX * rotateSpeed, 0);
}
//Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
//Moves the controller
controller.Move(moveDirection * Time.deltaTime);
}