Separate speeds for vertical and horizontal movement

Hi,

Please can someone smarter than me help me figure this out? Its giving me real trouble.

I’m looking for a way to have 8-directional movement, but with separate movement speeds when moving vertically and horizontally.

For example:

If the player presses ‘A’ or ‘D’ the character will move (left or right) at speed ‘x’ (normal walking speed)
But if the player presses ‘W’ or ‘S’ the character will move (up or down) at speed ‘y’ (slower than normal walking speed).

Basically I wish to make a 2.5D game (almost side-view but still slightly top-view) where the player can move in 8 directions, but any vertical movement is slower than any horizontal movement, to create the illusion of perspective (walking into the background or foreground).

I would really appreciate any advice on this as I can’t find anything that helps.

Thanks,

Ross

float hMoveSpeed = 1;
float vMoveSpeed = 1.5f;

float hMovement = Input.GetAxis("Horizontal") * hMoveSpeed;
float vMovement = Input.GetAxis("Vertical") * vMoveSpeed;

transform.Translate(new Vector3(hMovement, 0, vMovement);
1 Like

Thanks for such a fast response LaneFox!

I’ve tried the code but I’m getting compiler errors:

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

public class PlayerMovement : MonoBehaviour {

float hMoveSpeed = 1;
float vMoveSpeed = 1.5f;

float hMovement = Input.GetAxis(“Horizontal”) * hMoveSpeed;
float vMovement = Input.GetAxis(“Vertical”) * vMoveSpeed;

transform.Translate(new Vector3(hMovement, 0, vMovement));
}

I noticed there was a ‘)’ missing so I added it at the end but I’m still getting compiler errors.

Any ideas? Thanks for you help with this!

Change:
transform.Translate(new Vector3(hMovement, 0, vMovement));
To:
transform.Translate(new Vector3(hMovement, vMovement, 0));

as it is x, y, z not x, z, y. This won’t fix any compile errors to be able to do that we need to see the error

Also traslate is the wrong methoud to use, use transform.position = transform.position + new Vector3(hMovement, vMovement, 0) or gameObject.getComponent().movePosition(transform.position + new Vector3(hMovement, vMovement, 0)) if you are using a rigidbody. This should fix the error

Thank you for the responses.

My code currently looks like this:


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

public class PlayerMovement : MonoBehaviour {

float hMoveSpeed = 1;
float vMoveSpeed = 1.5f;

float hMovement = Input.GetAxis(“Horizontal”) * hMoveSpeed;
float vMovement = Input.GetAxis(“Vertical”) * vMoveSpeed;

gameObject.getComponent().movePosition(transform.position + new Vector3(hMovement, vMovement, 0));
}


However, I’m still getting the following compiler errors:

Assets/Scripts/PlayerMovement.cs(13,38): error CS1519: Unexpected symbol `(’ in class, struct, or interface member declaration

Assets/Scripts/PlayerMovement.cs(13,72): error CS1001: Unexpected symbol `+', expecting identifier

Assets/Scripts/PlayerMovement.cs(13,106): error CS1001: Unexpected symbol `,', expecting identifier

Assets/Scripts/PlayerMovement.cs(13,108): error CS1001: Unexpected symbol `0’, expecting identifier

Assets/Scripts/PlayerMovement.cs(13,41): error CS1520: Class, struct, or interface method must have a return type

Assets/Scripts/PlayerMovement.cs(13,110): error CS1001: Unexpected symbol `)', expecting identifier

Update: I’ve changed my code as I think I figured out why I was getting most of the compiler errors.

My code is now:


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

public class PlayerMovement : MonoBehaviour {

Rigidbody2D rbody;

void Start () {

rbody = GetComponent ();
}

void Update () {

float hMoveSpeed = 1;
float vMoveSpeed = 1.5f;

float hMovement = Input.GetAxis(“Horizontal”) * hMoveSpeed;
float vMovement = Input.GetAxis(“Vertical”) * vMoveSpeed;

gameObject.getComponent().movePosition(transform.position + new Vector3(hMovement, vMovement, 0));

}
}


I am now just getting one compiler error:

Assets/Scripts/PlayerMovement.cs(23,13): error CS1061: Type UnityEngine.GameObject' does not contain a definition for getComponent’ and no extension method getComponent' of type UnityEngine.GameObject’ could be found. Are you missing an assembly reference?

If you can’t resolve the compiler errors you’re getting from this then you need to start here.

Its OK I figured it out. Thanks for your help both of you.