- Home /
My Character is moving fine horizontally and vertically, but moves way too fast diagonally
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
public float moveSpeed;
private Animator anim;
private bool PlayerMoving;
private Vector2 LastMove;
private Rigidbody2D myRigidBody;
// Use this for initialization
void Start () {
myRigidBody = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
PlayerMoving = false;
if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f )
{
// transform.Translate (new Vector3(Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime, 0f, 0f));
myRigidBody.velocity = new Vector2(Input.GetAxisRaw("Horizontal"), myRigidBody.velocity.y) * moveSpeed;
PlayerMoving = true;
LastMove = new Vector2(Input.GetAxisRaw("Horizontal"), 0f);
}
if (Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f)
{
// transform.Translate(new Vector3(0f, Input.GetAxisRaw("Vertical") * moveSpeed * Time.deltaTime, 0f));
myRigidBody.velocity = new Vector2(myRigidBody.velocity.x, Input.GetAxisRaw("Vertical")) * moveSpeed;
PlayerMoving = true;
LastMove = new Vector2(0f, Input.GetAxisRaw("Vertical"));
}
if (Input.GetAxisRaw("Horizontal") < 0.5f && Input.GetAxisRaw("Horizontal") > -0.5f)
{
myRigidBody.velocity = new Vector2(0f, myRigidBody.velocity.y);
}
if (Input.GetAxisRaw("Vertical") < 0.5f && Input.GetAxisRaw("Vertical") > -0.5f)
{
myRigidBody.velocity = new Vector2(myRigidBody.velocity.x, 0f);
}
anim.SetFloat("moveX", Input.GetAxisRaw("Horizontal"));
anim.SetFloat("moveY", Input.GetAxisRaw("Vertical"));
anim.SetFloat("LastMoveX", LastMove.x);
anim.SetFloat("LastMoveY", LastMove.y);
anim.SetBool("PlayerMoving", PlayerMoving);
}
}
If you want direct control of the velocity every frame you should make the Rigidbody $$anonymous$$inematic and use Transform.Translate ins$$anonymous$$d. Otherwise you should use Rigidbody2D.AddForce in FixedUpdate. As a sidenote you should cache the Hash for the animator parameters ins$$anonymous$$d of using a string lookup every frame.
Answer by rabirland · Jan 03, 2017 at 02:37 PM
Use a temporal Vector3 to store the velocity. At the end of the Update, set the real velocity to: tmpVelocity.normalized * moveSpeed;
You can do as rabirland said.
The reason why you are now moving twice as fast is because you are basically applying two forces when you move diagonally and only 1 when you strictly move horizon or vertical. Two forces are being applied because of the if statements.
Not quite, normalizing a vector forces it's length to 1. For example, Normalize(0f, 0.00001f, 0) becomes (0, 1, 0).
What you should use ins$$anonymous$$d is Vector3.Clamp$$anonymous$$agnitude. This will clamp the length of the vector to a value (In this case, 1), but will not cause it to grow if it is very small.
Answer by Arycama · Apr 24, 2017 at 11:08 AM
Make a local Vector3 variable called "velocity". instead of setting myRigidBody.velocity in the first two if statements, set the x and y values of velocity instead, and -don't- multiply it by rotation speed.
Finally before setting myRigidBody.velocity, use Vector3.ClampMagnitude to clamp the total of the vector to 1.
Here's an example, please note that I left out the other parts of the code as they are not related to the answer. Also I'm using Mathf.Abs for clarity, instead of two if statements, though this is not essential:
void Update()
{
var velocity = new Vector3();
if (Mathf.Abs(Input.GetAxisRaw("Horizontal")) > 0.5f)
{
velocity.x = Input.GetAxisRaw("Horizontal");
}
if (Mathf.Abs(Input.GetAxisRaw("Vertical")) > 0.5f)
{
velocity.y = Input.GetAxisRaw("Vertical");
}
// This is where the magic happens
velocity = Vector3.ClampMagnitude(velocity, 1) * moveSpeed;
}
Your answer
Follow this Question
Related Questions
GameObject wont stop rotating ? 1 Answer
character(ball) rolls with less speed when I build it 2 Answers
Movement speed difference between the different devices 1 Answer
Can't change the speed of a character. 2 Answers
Increasing the speed of an object when the scale is decreased and vice versa 0 Answers