I am trying to make a first-person-controlled, zero-gravity environment where you pretty much move in the direction you are facing, current velocity willing. (I’m using rigidbody movement) The scripts I am about to display work almost exactly how I want, except that the forward movement when looking up or down from the world X-Z plane doesn’t move to where I am looking. For example, if I hit the start button and don’t move the mouse to keep looking forward and hold W, I will move exactly forward. But if I move my mouse upward and and looking more vertically, when I hold W down, I move at less of an upward angle than where I am looking. How can I fix that?
For mouse look I am using this script:
// Performs a mouse look.
var horizontalSpeed : float = 2.0;
var verticalSpeed : float = 2.0;
function FixedUpdate () {
// Get the mouse delta. This is not in the range -1...1
var h : float = horizontalSpeed * Input.GetAxis ("Mouse X");
var v : float = verticalSpeed * Input.GetAxis ("Mouse Y");
transform.Rotate (-v, h, 0);
}
For the character controller I am using this script:
// These variables are for adjusting in the inspector how the object behaves
var maxSpeed = 7.000;
var force = 8.000;
var jumpSpeed = 5.000;
// These variables are there for use by the script and don't need to be edited
private var state = 0;
private var grounded = false;
private var jumpLimit = 0;
// Don't let the Physics Engine rotate this physics object so it doesn't fall over when running
function Awake ()
{
rigidbody.freezeRotation = true;
}
// This part detects whether or not the object is grounded and stores it in a variable
function OnCollisionEnter ()
{
state ++;
if(state > 0)
{
grounded = true;
}
}
function OnCollisionExit ()
{
state --;
if(state < 1)
{
grounded = false;
state = 0;
}
}
// This is called every physics frame
function FixedUpdate ()
{
// Get the input and set variables for it
jump = Input.GetButtonDown ("Jump");
horizontal = Input.GetAxis("Horizontal");
vertical = Input.GetAxis("Vertical");
// Set the movement input to be the force to apply to the player every frame
horizontal *= force;
vertical *= force;
// If the object is grounded and isn't moving at the max speed or higher apply force to move it
if(rigidbody.velocity.magnitude < maxSpeed)
{
rigidbody.AddForce (transform.rotation * Vector3.forward * vertical);
rigidbody.AddForce (transform.rotation * Vector3.right * horizontal);
}
// This part is for jumping. I only let jump force be applied every 10 physics frames so
// the player can't somehow get a huge velocity due to multiple jumps in a very short time
if(jumpLimit < 10) jumpLimit ++;
if(jump && grounded == true && jumpLimit >= 10)
{
rigidbody.velocity.y += jumpSpeed;
jumpLimit = 0;
}
}