hey guys,
I’m trying to make some kinda of roll a ball game in 3rd person. The ball moves forward, backwards, to the left or right depending on the input you give. The movement is based on where the camera is facing(I can move my mouse to change the camera’s position). Im moving my ball with addforce, but I’m unable to get the ball to move in the right way. The only way Ive been able to manage to get the movement somewhat right is by setting angles and rotating the vectors, but I want it by vectors
this is the code by angles:
float angle = 0;
isMoving = false;
if (Input.GetKey(InputManager.IM.forward))
{
angle += 0;
isMoving = true;
}
// transform.position += Vector3.forward / 2;
if (Input.GetKey(InputManager.IM.backward))
{
angle += 180;
isMoving = true;
}
// transform.position += -Vector3.forward / 2;
if (Input.GetKey(InputManager.IM.left))
{
angle -= 90;
isMoving = true;
}
//transform.position += Vector3.left / 2;
if (Input.GetKey(InputManager.IM.right))
{
angle += 90;
isMoving = true;
}
angle *= Mathf.Deg2Rad;
// transform.position += Vector3.right / 2;
hDir = MainCamera.transform.forward;
hDir.y = 0;
Force = new Vector3(hDir.x * Mathf.Cos(angle) + hDir.z * Mathf.Sin(angle), 0, -hDir.x * Mathf.Sin(angle) + hDir.z * Mathf.Cos(angle));
if (isMoving == true)
{
rb.AddForce(Force * 1000);
}
rb.velocity *= 0.5f;
and this is what I want:
float x = 0;
float z = 0;
if (Input.GetKey(InputManager.IM.forward))
{
z += 1;
}
// transform.position += Vector3.forward / 2;
if (Input.GetKey(InputManager.IM.backward))
{
z -= 1;
}
// transform.position += -Vector3.forward / 2;
if (Input.GetKey(InputManager.IM.left))
{
x -= 1;
}
//transform.position += Vector3.left / 2;
if (Input.GetKey(InputManager.IM.right))
{
x += 1;
}
hDir = MainCamera.transform.forward;
hDir.y = 0;
Force = ??????
if (x != 0 || z != 0)
{
rb.AddForce(Force * 1000);
}
rb.velocity *= 0.5f;
*/
Any help is highly appriciated