Move ball player relative to camera rotation
I am working on a game where the player controls a free moving and rotating ball. I am unable to determine the Vector the player needs to correct to when the camera has been rotated. I can't make the camera a child of the player because is would follow its every rotation. What I think I have to do is edit my player's movement vector based off of the angle the camera is facing the player from.
What should I do to fix this problem?
Here is the code for the camera at the moment:
private Vector3 offset:
public GameObject target;
float RotateSpeed = 3.0f;
void Start()
{
offset = gameObject.transform.position - target.transform.position;
}
private void FixedUpdate()
{
if (Input.GetKey(KeyCode.Q))
{
Quaternion turnAngle = Quaternion.AngleAxis(-RotateSpeed, Vector3.up);
offset = turnAngle * offset;
}
else if (Input.GetKey(KeyCode.E))
{
Quaternion turnAngle = Quaternion.AngleAxis(RotateSpeed, Vector3.up);
offset = turnAngle * offset;
}
gameObject.transform.position = target.transform.position + offset;
}
void Update()
{
gameObject.transform.LookAt(target.transform);
}
And here is the current code I have on the player, excluding my attempts to correct the problem:
public int speed;
private void FixedUpdate()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Vector3 playerDirection = new Vector3(h*speed,0,v*speed);
gameObject.GetComponent<Rigidbody>().AddForce(playerDirection);
//this if statement freezes the ball for the player by pressing X.
if (Input.GetKey(KeyCode.X))
{
Rigidbody rb = gameObject.GetComponent<Rigidbody>();
rb.velocity = new Vector3(0, 0, 0);
}
}
Your answer
Follow this Question
Related Questions
Make character face movement direction 0 Answers
Camera Quaternion.RotateTowards without rolling/banking 1 Answer
How to determine one point (Vector3) based on 7 other points (Vector3's) with 2 coordinate systems? 1 Answer
Get object's rotation around axis defined by vector 2 Answers
Weapon Spray issue 1 Answer