Need to disable diagonal movement and add grid walking
Hello,
I'm pretty new to gamedesign, so I'm not entirely sure what I'm doing. I have used Brackeys' tutorial for top down movement (the code below), but as the title states, I don't want to be able to walk diagonally. I also want my player to walk in a grid pattern. I have found a tutorial that adresses both those issues (by gamesplusjames), but the whole code involving movement and most importantly, involving the animations is totally different. Does anyone know how best to do this?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 2f;
public Rigidbody2D rb;
public Animator animator;
Vector2 movement;
//Input
void Update()
{
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
animator.SetFloat("Horizontal", movement.x);
animator.SetFloat("Vertical", movement.y);
animator.SetFloat("Speed", movement.sqrMagnitude);
}
//Movement
private void FixedUpdate()
{
rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
}
}
Your answer
Follow this Question
Related Questions
Grid Movement with Smooth Movement 0 Answers
Controlling Angle of Movement When Holding 2 Arrow Keys - 2d Top Down 0 Answers
How could I implement diagonal movement in this code? 0 Answers
Need help to solve problems with First Person Dungeon Crawler movement script 0 Answers
Character Diagonal Movement Issue 3 Answers