Hello there! This is my first post, so go easy on me.
I’m trying to make a ball (sphere) to roll in all directions. I’ve currently written a script so I can get it moving north, south, west, and east but only one at a time. I want the player to be able to get the ball rolling e.g. southeast, northwest, etc… I’ve been looking all around, but maybe not the right places?
I really hope you guys can help me! Thank you
Below is my code
public class Controller : MonoBehaviour
{
Vector3 input;
Rigidbody m_Rigidbody;
public float m_Speed = 2.0f;
private string direction;
void Awake()
{
m_Rigidbody = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate()
{
GetInput();
if (Mathf.Abs(input.x) < 1 && Mathf.Abs(input.y) < 1) return;
{
print(input);
if (Input.GetKey(KeyCode.S))
{
m_Rigidbody.velocity = (Vector3.forward) * (m_Speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.A))
{
m_Rigidbody.velocity = Vector3.right * (m_Speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.W))
{
m_Rigidbody.velocity = Vector3.back * (m_Speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.D))
{
m_Rigidbody.velocity = Vector3.left * (m_Speed * Time.deltaTime);
}
}
}
void GetInput()
{
input.x = Input.GetAxisRaw("Horizontal");
input.y = Input.GetAxisRaw("Vertical");
}
}
6127259–667904–Controller.cs (1.34 KB)