So as the title suggests, I’m looking to find a way to add force to a rigid-body, based on the direction of another object, using player input. For example, if the player pressed w then it would move towards the forward direction the other game object is looking. Same for a, s and d respectively.
This is what I have so far. Please note that is is designed to work in VR hence the first two lines. This shouldn’t affect the input as I am using D-pad input.
//Set size and position of the capsule collider so it maches our head.
Collider.height = Head.transform.localPosition.y;
Collider.center = new Vector3(Head.transform.localPosition.x, Head.transform.localPosition.y / 2, Head.transform.localPosition.z);
moveDirection = new Vector3(0, 0, 0);//(Head.transform.forward*trackpad);
if (StandardisedControlScript.Dpad(Hand, "w"))
{
moveDirection = new Vector3(moveDirection.x+1, moveDirection.y, moveDirection.z);
}
if (StandardisedControlScript.Dpad(Hand, "a"))
{
moveDirection = new Vector3(moveDirection.x, moveDirection.y, moveDirection.z+1);
}
if (StandardisedControlScript.Dpad(Hand, "s"))
{
moveDirection = new Vector3(moveDirection.x-1, moveDirection.y, moveDirection.z);
}
if (StandardisedControlScript.Dpad(Hand, "d"))
{
moveDirection = new Vector3(moveDirection.x, moveDirection.y, moveDirection.z-1);
}
gameObject.transform.rotation = Quaternion.Euler(new Vector3(0, Head.transform.rotation.y, 0));
Debug.Log(moveDirection);
updateInput();
//GetComponent<Rigidbody>().MoveRotation(Quaternion.Euler(new Vector3(0, Head.transform.rotation.y, 0)));
GetComponent<Rigidbody>().velocity = new Vector3(GetComponent<Rigidbody>().velocity.x/2, GetComponent<Rigidbody>().velocity.y, GetComponent<Rigidbody>().velocity.z / 2);
GetComponent<Rigidbody>().AddRelativeForce(moveDirection * accellSpeed);
Any help will be appreciated. I’ve tried using AddForce and AddRelative force but despite much googling, I’m not sure I have been implementing them correctly.
If you need more info on anything, please ask and I’ll get back to you ASAP
Works a treat. I’ll go about converting it to work with my current system etc and let you know if there are any issues. Thanks for putting in the time to write that up