How to get rid of double movement speed?
when i press the 'w' key my player moves at the right speed but when i press 'w' and 'a' or 'd' at the same time my play runs much faster
My Script: using UnityEngine; using System.Collections;
public class PlayerMovement2 : MonoBehaviour {
public GameObject Camera_Point;
public float movespeed = 5.0f;
public float MouseXSensitivity = 5f;
public float MouseYSensitivity = 3f;
public GameObject _player;
float verticalRotation = 0;
public float upDownRange = 60.0f;
public float verticalVelocity = 5f;
public float jumpSpeed = 5.0f;
CharacterController characterController;
Animator anim;
void Start ()
{
anim = GetComponent<Animator>();
characterController = GetComponent<CharacterController>();
_player = gameObject;
}
void Update ()
{
PlayerMovement();
Animation();
}
void Animation()
{
anim.SetFloat("InputZ", Input.GetAxis("Vertical"));
anim.SetFloat("InputX", Input.GetAxis("Horizontal"));
}
void PlayerMovement()
{
float rotLeftRight = Input.GetAxis("Mouse X") * MouseXSensitivity;
transform.Rotate(0, rotLeftRight, 0);
verticalRotation -= Input.GetAxis("Mouse Y") * MouseYSensitivity;
verticalRotation = Mathf.Clamp(verticalRotation, -upDownRange, upDownRange);
Camera_Point.transform.localRotation = Quaternion.Euler(verticalRotation, 0, 0);
//Player Movement
movespeed
float forwardSpeed = Input.GetAxis("Vertical") * movespeed;
float sideSpeed = Input.GetAxis("Horizontal") * movespeed;
verticalVelocity += Physics.gravity.y * Time.deltaTime;
if (characterController.isGrounded && Input.GetButton("Jump"))
{
verticalVelocity = jumpSpeed;
}
Vector3 speed = new Vector3(sideSpeed, verticalVelocity, forwardSpeed);
speed = transform.rotation * speed;
//Running
if (Input.GetKey(KeyCode.LeftShift))
{
movespeed = 15;
}
else
{
movespeed = 5;
}
characterController.Move(speed * Time.deltaTime);
}
}
Answer by Kamil1064 · May 31, 2016 at 06:23 AM
Hi @The Evster, that's happening because you get an velocity in forward and right in the same time. Here is little example how to set up this:
float xMov = Input.GetAxisRaw("Vertical");
float zMov = Input.GetAxisRaw("Horizontal");
Vector3 mHorizontal = transform.right * xMov;
Vector3 mVertical = transform.forward * -zMov;
Vector3 velocity = (mHorizontal + mVertical).normalized * moveSpeed;
Just use last variable to move your character.
That's not really a good approach since no matter how "strong" an input is you always run at max speed due to normalization.
It's easier to use Vector.Clamp$$anonymous$$agnitude. It only restricts the magnitude of the vector if it's greater than the given magnitude.
For this you should first combine all lateral movement into one vector and then clamp the result. Finally add the verticalVelocity.
So specifically just do:
Vector3 speed = new Vector3(sideSpeed, 0, forwardSpeed);
speed = Vector3.Clamp$$anonymous$$agnitude(speed, movespeed);
speed.y = verticalVelocity;
speed = transform.rotation * speed;
Your answer
![](https://koobas.hobune.stream/wayback/20220612081934im_/https://answers.unity.com/themes/thub/images/avi.jpg)