Hello, I am new to Unity and Coding in general, and I had a bit of a struggle with diagonal movement of my Player in a top down 2D environment (Think RPG). I think I finally figured it out and thought I would share it for document my own progress and to help other newbies that have no clue. Wading through all the unhelpful half solutions was really off putting, so here’s what I came up with, maybe it helps some lost starter:
using UnityEngine;
public class WalkingScript : MonoBehaviour
{
public Rigidbody2D BodyPlayer;
private float horizontal;
private float vertical;
public float Speed = 350;
private Vector3 movement;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
horizontal = Input.GetAxisRaw("Horizontal");
vertical = Input.GetAxisRaw("Vertical");
}
private void FixedUpdate()
{
movement = new Vector3(horizontal, vertical, 0).normalized;
BodyPlayer.velocity = new Vector3(movement.x * Speed, movement.y * Speed, movement.z * Speed) * Time.fixedDeltaTime;
Debug.Log(movement);
}
}
Here’s the same code with remarks how I understood the functions and how they work. I beg for corrections and optimisations, I’m new and uneducated, and honestly, the Unity documentation is pretty bad if you’re not coming froma coding background.
using UnityEngine;
public class WalkingScript : MonoBehaviour // It's this Script
{
public Rigidbody2D BodyPlayer; // You have to give your Character Physics first in Unity so you can use it in this Script.
private float horizontal; // declaring the variable where the horizontal Player Input will be stored.
private float vertical; // same for the vertical Input
public float Speed = 350; // variable for the Player Speed. I set it to public so I can change it during testing.
private Vector3 movement; // creating a variable that can store information of the position x, y and z.
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
horizontal = Input.GetAxisRaw("Horizontal"); // stores the user input into the variable horizontal
vertical = Input.GetAxisRaw("Vertical"); // same for vertical inputs
}
private void FixedUpdate()
{
movement = new Vector3(horizontal, vertical, 0).normalized; // this takes the values stored in horizontal and vertical and makes them always add to 1, so diagonal movement isnt faster than horizontal or vertical
BodyPlayer.velocity = new Vector3(movement.x * Speed, movement.y * Speed, movement.z * Speed) * Time.fixedDeltaTime; // this moves the Player Sprite by applying new values to the coordinates of it. The normalized vectors that are stored in 'movement' are used and applied to each axis of the player. DeltaTime makes your Speed tied to a fixed Clock rather than the processor, so it does run the same speed on all machines.
Debug.Log(movement); // this shows you the normalized current values in the debug window when you run the scene
}
}
Bonus - My Code that also includes a running function:
using UnityEngine;
public class WalkingScript : MonoBehaviour
{
public Rigidbody2D BodyPlayer;
public float WalkingSpeed = 850;
public float RunningSpeed = 1125;
private float horizontal;
private float vertical;
private float Speed;
private Vector3 movement;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
horizontal = Input.GetAxisRaw("Horizontal");
vertical = Input.GetAxisRaw("Vertical");
if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.Joystick1Button1))
{
Speed = RunningSpeed;
}
else
{
Speed = WalkingSpeed;
}
}
private void FixedUpdate()
{
movement = new Vector3(horizontal, vertical, 0).normalized;
BodyPlayer.velocity = new Vector3(movement.x * Speed, movement.y * Speed, movement.z * Speed) * Time.fixedDeltaTime;
Debug.Log(BodyPlayer.velocity);
Debug.Log(movement);
}
}