How do i find player rotation and convert that into a force facing that direction.
I am making a script that allows a player to pick up and throw objects. i have got it working so far, the player can pick up the object and move it around, then when you release the mouse it throws. ive got the throw working using addforce but it only goes in one direction and it doesnt matter where the player is facing.
the script i have so far
public class PickUp : MonoBehaviour { public Rigidbody rb; public Transform target; public float speed = 10;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
}
void OnMouseDown()
{
this.transform.position = target.position;
this.transform.parent = GameObject.Find("FPSController").transform;
this.transform.parent = GameObject.Find("FirstPersonCharacter").transform;
rb.isKinematic = true;
rb.detectCollisions = false;
}
void OnMouseUp()
{
this.transform.parent = GameObject.Find("FPSController").transform;
this.transform.parent = null; // unparent the GameObject From The Target
rb.isKinematic = false;
rb.detectCollisions = true;
Vector3 throwforce = new Vector3(transform.rotation, 500, 700);
rb.AddForce(throwforce);
this.transform.parent = null;
}
}
The script is also applied individually to each object and not a child of the player, it kind of seems inefficient to me to have multiple copies of the script one for each object.
Vector3 throwforce = new Vector3(transform.rotation, 500, 700);
This line is where the force is added but obviously not relative to where the player is facing
If anyone knows where i should start then i will be very grateful!
Your answer
Follow this Question
Related Questions
AddForce in direction of different object? 1 Answer
Why does my rigidbody move it's transform while rotating? 1 Answer
Mesh starts forcing from the wrong axis position 0 Answers
Torque rigidbody toward desired rotation on two axes ignoring Y axes? 0 Answers
How do I get a Rigidbody to rotate in the direction it is moving? 0 Answers