- Home /
How to change Add.Force direction depending on the camera angle in the XZ plane
Dear reader,
I got stuck, in what may appear to become a cumbersome game in Unity. Actually, it is cumbersome. However, I'm trying to learn how to code c#, and cannot seem to factor in the camera angle in my applied force to my player / object. Here's the ballgame: Through an add.Force, applied by pressing WASD keys a cube (the player) is basically hit in that certain forward, left, backward or right direction. It therefore starts 'rolling', of course a little out of control - that's the idea. When it hits an object, the cube transforms its shape, randomly, in its xyz, making it worse to control in most cases. This all works dandy, albeit probably devious at best.
For instance the control for forwards would be:
if(Input.GetKey("w"))
{
rb.AddForce(0,0,forwardForce * Time.deltaTime);
}
Second, I have managed to orbit the main camera around the player, following from a safe distance, controlled by the pointer / mouse.
Now my problem is this: moving the cube / player forward (or left, etc) will always be the same direction (z-axis of the project), independent on the angle of the orbiting camera, but, basically I want the two coordinate systems 'to merge'. So if the camera is orbiting left, the angle of the applied force will also be redirected. Meaning, pressing W, the add force will not be in the z-direction of the project anymore, but in the z direction of the camera.
I have tried heaps, using empty GameObjects in between, trying to subtracting Quaterions using Input.GetAxis("Mouse X") to find values. But I simply must admit I just really don't get it! Is there anybody here that knows the answer? The question is either how to integrate both coordinate systems, or maybe how to provide a new angle to the already existing force.
Sincerely,