I'm making a rigidbody movement script, and I've made it so you can set forwards, backwards and strafing speeds to be different. But I have a bit of a problem, when I walk backwards and strafe I travel at 2 units per second, when I should be on travelling 1.75 based on the setting?
Here is my script.
using UnityEngine;
using System.Collections;
public class RigidbodyMovement : MonoBehaviour {
public float forwardsSpeed;
public float backwardsSpeed;
public float strafeSpeed;
bool movingBackwards;
float verticalSpeed;
void Start ()
{
}
void Update ()
{
}
void FixedUpdate ()
{
movingBackwards = Input.GetAxis ("Vertical") <= 0 ? true : false;
verticalSpeed = movingBackwards == true ? backwardsSpeed : forwardsSpeed;
Vector3 velocity = new Vector3 (Input.GetAxis ("Horizontal") * (strafeSpeed / forwardsSpeed), 0, Input.GetAxis ("Vertical") * (verticalSpeed / forwardsSpeed));
velocity = velocity.magnitude > 1 ? Vector3.Normalize (velocity) : velocity;
velocity.x *= strafeSpeed / (strafeSpeed / forwardsSpeed);
velocity.z *= verticalSpeed / (verticalSpeed / forwardsSpeed);
rigidbody.velocity = transform.TransformDirection (velocity);
}
}
My settings for the public variables are, forwardsSpeed = 2, backwardsSpeed = 1.75, strafeSpeed = 1.5. When I walk forwards I travel at 2m/s, when I walk backwards I travel at 1.75m/s and when I strafe I walk 1.5m/s, this is how it should be. When I walk forwards and strafe I still travel 2m/s, all good, however when I walk backwards and strafe I travel 2m/s which is not what I want at all, I should only travel 1.75m/s? Why is that? When I have strafe speed and backwards speed both set to 1 I travel at root 2 (1.414...) which makes me think it's not being properly normalized?
I can't figure out why this is happening, I've tried many different things.
Can somebody please give me a solution?
↧