- Home /
Make movement vertically,How to move Vertically?
I am currently working on having an object being detected in certain grid and count how long. For now I am just working on having an object that is able to move vertically and horizontally. I made an object that is able to move horizontally however I cannot get it to move vertically. It only moves vertically when the gravity is set to 1. My goal is for it to be set to 0 and have the ability to move vertically and horizontally. By vertically I do not mean jump. Or is this task impossible? Just wanted to clarify that I'm new to unity. Here is the code
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Move2D : MonoBehaviour{ public float moveSpeed = 5f; // Start is called before the first frame update void Start() {
}
// Update is called once per frame
void Update()
{
Vector3 movement = new Vector3(Input.GetAxis("Horizontal"),0f,0f);
//Vector3 move_up = new Vector3(Input.GetAxis("Vertical"),0f,0f);
// tried to add the code above for movement vertical does not work.
transform.position += movement * Time.deltaTime * moveSpeed;
}
}
Answer by NoahDaNerd · Aug 09, 2020 at 10:28 PM
I think the problem is that you are setting move_up to have the vertical input as the x of the vector3, not the y.
This should work:
void Update()
{
Vector3 movement = new Vector3 (Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0f);
transform.position += movement * Time.deltaTime * moveSpeed;
}
Your answer
Follow this Question
Related Questions
How to lock element width or height in a vertical or horizontal layout? 1 Answer
Play animation for horizontal and vertical movement? 1 Answer
Creating a 2D Movement Grid 0 Answers
Grid Movement VS Free Movement 1 Answer
How Can I Create a Button that Takes Multiple Objects and cycles them through a grid? (Unity 2D) 0 Answers