- Home /
How to move a gameobject on a grid
Hi all,
I have struggling to move the player ball in the following grid based map. I tried rigidbody addForce and transform.Translate but nothing worked.
void FixedUpdate()
{
if (Input.GetKey(KeyCode.LeftArrow))
{
int indicator = PlayerPrefs.GetInt("indicator", 0);
if(indicator == 1)
{
turn = true;
m_Rigidbody.AddForce(direction * 0);
direction = Vector3.left;
m_Rigidbody.AddForce(direction * m_Speed);
turn = false;
}
}
if (Input.GetKey(KeyCode.RightArrow))
{
int indicator = PlayerPrefs.GetInt("indicator", 0);
if (indicator == 1)
{
turn = true;
m_Rigidbody.AddForce(direction * 0);
direction = Vector3.right;
m_Rigidbody.AddForce(direction * m_Speed);
turn = false;
}
}
if (!turn)
{
m_Rigidbody.AddForce(direction * m_Speed);
}
}
Green color sphere is my player object. I want player to continuously move forward (first in +z axis). Then when the user press left or right arrow keys, i want the ball to turn left or right and continue in that direction.
Answer by AnOrdinarySandwich · Jun 09, 2021 at 05:30 PM
How about changing the velocity of the rigidbody directly? This will allow you to "stop" the ball traveling in one direction (which is what I'm assuming the code is doing by "direction * 0") by changing its actual velocity parameters.
Your answer
Follow this Question
Related Questions
Restricting NavMeshAgent to not travel in diagonals 0 Answers
Automatic nav mesh generation 2 Answers
Getting a navmeshagent back on a navmesh. 1 Answer
Draw path along Navmesh agent path 3 Answers
How do I get agents to avoid each other? 0 Answers