- Home /
Moving player relative to non-child camera
The goal: I want to move the player in reference to a 3rd person camera that is not a child of the player model.
Rundown: I have a player that rotates to keep its back to the camera (y axis only). The camera(main) is situated behind and above the player and pivots around an empty that is co-located with the player model.
I cannot parent the player model to the camera or vice versa as it rotates at a slower rate while the camera is unrestricted in speed. For example, like a slow turret traversing to track the cursor.
I've attempted to use AddForce and AddRelativeForce to move the player. However, these moved the player in reference to the world's rotation (AddForce) or the player model's rotation (AddRelativeForce).
Here's my current code for this segment (attached to player model):
void Move (){
Rigidbody rb = GetComponent<Rigidbody> ();
Vector3 conDir = Camera.main.transform.forward;
conDir = new Vector3 (Input.GetAxisRaw ("Horizontal"), 0f, Input.GetAxisRaw ("Vertical"));
rb.AddRelativeForce (conDir.normalized * mSpeed);
}
Answer by ChocoCrispi · Oct 03, 2017 at 02:33 AM
Solved with:
void Move (){
Rigidbody rb = GetComponent<Rigidbody> ();
var pEmpty = GameObject.Find ("PlayerEmpty");
//Forward-Back
Vector3 FBAxis = pEmpty.transform.forward;
float FBForce = Input.GetAxisRaw ("Vertical");
rb.AddForce (FBAxis * FBForce * mSpeed);
//Left-Right
Vector3 LRAxis = pEmpty.transform.right;
float LRForce = Input.GetAxisRaw ("Horizontal");
rb.AddForce (LRAxis * LRForce * mSpeed);
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Player movement with lerp is jerky. 1 Answer
Distribute terrain in zones 3 Answers