Jump to content

I need some help with Unity player movement

So I'm trying to make a Zelda-like top down RPG and I need a way to disable diagonal movement. I'm using a RigidBody to control the character. Here's the script, yes I know animations are incomplete. 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour {

	public float moveSpeed; 
	private Animator anim;
	private Rigidbody2D rbody;

	// Use this for initialization
	void Start () {
		anim = GetComponent<Animator> ();
		rbody = GetComponent<Rigidbody2D> ();
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f )
		{
			//transform.Translate (new Vector3(Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime, 0f, 0f ));
			rbody.velocity = new Vector2(Input.GetAxisRaw("Horizontal") * moveSpeed, rbody.velocity.y);
		}
		if (Input.GetAxisRaw ("Vertical") > 0.5f || Input.GetAxisRaw ("Vertical") < -0.5f) 
		{
			//transform.Translate (new Vector3 (0f, Input.GetAxisRaw ("Vertical") * moveSpeed * Time.deltaTime, 0f));
			rbody.velocity = new Vector2(rbody.velocity.x, Input.GetAxisRaw("Vertical") * moveSpeed);
		}

		if (Input.GetAxisRaw ("Horizontal") < 0.5f && Input.GetAxisRaw ("Horizontal") > -0.5f) 
		{
			rbody.velocity = new Vector2(0f, rbody.velocity.y);
		}
		if (Input.GetAxisRaw ("Vertical") < 0.5f && Input.GetAxisRaw ("Vertical") > -0.5f) 
		{
			rbody.velocity = new Vector2(rbody.velocity.x, 0f);
		}

		anim.SetFloat("MoveX", Input.GetAxisRaw ("Horizontal"));
		anim.SetFloat("MoveY", Input.GetAxisRaw ("Vertical"));
	}
}

 

Did my post help you? Then make sure to rate it!

Check out my post on symbolic links! || PSU ranking and tiers || Pokemon Thread

 

Link to comment
https://linustechtips.com/topic/750758-i-need-some-help-with-unity-player-movement/
Share on other sites

Link to post
Share on other sites

you could do an if statment comparing the vetical axis and horizontal  

 

something like 

 

if (mathf.Abs(Input.GetAxis("vertical")) > mathf.Abs(Input.GetAxis("horizontal")))

{

//vertial movement code

//i would use

transform.Translate(Vector3.forward * Time.deltaTime * Input.GetAxis("vertical"))

}else{

//horizontal movement code

transform.Translate(Vector3.right * Time.deltaTime * Input.GetAxis("horizontal"))

 

}

 

THE RIG 

CPU: i7 3770K @ 4.9 COOLER: Kraken X61 MOBO: Z77 Sabertooth RAM: Vengeance 16gb @1600 GPU: ASUS ROG Strix GTX 1080 PSU: EVGA Supernova G2 850w SSD: 850 Pro 256Gb    CASE: B/W phantom 410 AUDIO: Kraken 7.1 Chroma MOUSE: Death Adder Chroma KEYBOARD: Black Widow Chroma MONITOR: Triple 23" Apple Cinama Displays   VR: HTC Vive & Oculus Rift DK2 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×