- Home /
Add force in direction to camera
Hello, I need to get the camera forward direction without the camera tilt direction and convert the balls forward direction to it?
Current setup:
// Get horizontal and vertical inputs and assign to vector3 "move"
Vector3 move = new Vector3(BaseInput.getHorizontal, 0f, BaseInput.getVertical);
// Assign camera transform forward to the ball forward
move = camera.TransformDirection(move);
// Finally add force in the direction
rigidbody.AddForce(move * _speed);
I really need help on this. Thank you!
Answer by SirPaddow · Jun 05, 2019 at 05:17 PM
The tilt of your camera is the y value of your forward vector, so something like this should do the trick:
Vector3 move = new Vector3(camera.transform.forward.x * BaseInput.getHorizontal, 0, camera.transform.forward.z * BaseInput.getVertical);
Sorry, that won't work. Try this ins$$anonymous$$d:
Vector3 move = new Vector3(camera.forward.x, 0, camera.forward.z) * BaseInput.getVertical + new Vector3(camera.right.x, 0, camera.right.z) * BaseInput.getHorizontal;
Unfortunately the same result. I could probably wrap my head around it after sleep but no luck today as I am just pure confused on this topic.
Thanks for the help, to give additional information adding force to the right or the left works fine and the angle doesn't matter.
So really the only thing I need is to get the forward direction as a Vector3 with a value of 1, so for example " new Vector3(1,0,0) " and use that to multiply the move vector.
I'll come back later after rest, figure it out and share it.
Thanks anyways.
I still get the same results, when looking forward it adds more velocity than looking at a downwards angle of around 75 like in the picture sample, so it seems like the velocity is still depending on camera tilt.
Also I didn't mention this but my camera rotates forward on the X axis.
Ow, ok, if think I finaly understood! You want to get a move.magnitude == 1, right? That's pretty easy, you only need to call ".normalized" on it:
Vector3 forwardNoTilt = new Vector3(camera.transform.forward.x, 0, camera.transform.forward.z);
Vector3 normalizedForwardNoTilt = forwardNoTilt.normalized;
So, all together:
Vector3 forward$$anonymous$$ovement = new Vector3(camera.forward.x, 0, camera.forward.z).normalized * BaseInput.getVertical;
Vector3 lateral$$anonymous$$ovement = new Vector3(camera.right.x, 0, camera.right.z).normalized * BaseInput.getHorizontal;
Vector3 move = forward$$anonymous$$ovement + lateral$$anonymous$$ovement;
// If you normalize zero, behaviour can be weird
if (move != Vector3.zero)
{
rigidbody.AddForce(move.normalized * _speed);
}
Answer by YBHOLTZ · Jun 05, 2019 at 07:23 PM
If you want the camera direction forward relative a object, you must:
Vector3 dir = Target.transform.forward;
Vector3 fixed_dir = camera.transform.rotation * dir;
will be the corrected forward direction for the forward camera based on the object.
Hope this helps.
How would I implement this with my move (input of horizontal and vertical movement) with the direction multiplied by the speed (float) in the rigidbody.AddForce(); ? Thanks.
In this case, you would have to do the move in two steps:
First we solve the forward then the right
Then you would simply multiply BaseInput.getVertical by found forward and move as you are already doing.
Then multiply BaseInput.getHorizontal by found right and do the move again.
Something like this (in the same frame):
rigidbody.AddForce(fixed_forward * _speed);
rigidbody.AddForce(fixed_right * _speed);
But ideally you would move the camera by following another object. I mean, ins$$anonymous$$d of moving it yourself. The camera would only follow an object and you would do more "normal" moving the object and the camera would only follow
I'll try the first option because the second option is not possible as the player has to look around.
I am trying to achieve movement like in this video: https://www.youtube.com/watch?v=aviL3HX3UEc
Thanks.