- Home /
How to add force towards where the camera is pointing?
I'm trying to make a weapon fire but when I'm firing, it is shooting towards a certain point in the map, and if I move at an angle (holding W and A at the same time) it shoots towards the direction I'm walking.
I just need a thing to insert here to make it go towards where the camera (player) is pointed
bullet.rigidbody.AddForce (<THINGY HERE> * 20);
Also I'm using js to do this.
This is not a simple problem of inserting 'thingy here'. Unless you are shooting directly from the center of the screen and have gravity turned off, you will need to triangulate and/or compensate. That is, where you point the weapon will depend on at what distance you want the projectile to cross the center of the view of the camera, and if you want to do any compensation for the fact that a projectile falls due to gravity.
I just need something to put there as a direction where the camera is looking (center of the screen), like Vector3.forward (That's an example direction I can't use that).
Take a look at this diagram.
The hex is the camera. The triangle is where you are shooting/moving from. So to be able to shoot/go where the camera is looking, you have to answer the question at what distance. All the dots represent places where the camera is looking, but the object moving/shooting has to select one of them. If you know the distance, you can do something like this:
var point = Camera.main.transform.position + Camera.main.transform.forward * 10;
var dir = (point - bullet.transfrom.position).normalized;
bullet.AddForce(dir * 500);
This will shoot where the camera is looking 10 units from the camera.
robertbu, when I try that it spawns them in this one direction by me, and if I run forward it it shoots them in front of me, but not standing still.
VesuvianPrime, when I do that it works, but I can only shoot them in a 180 degree half-circle. So if I spin 360 degrees while shooting, it shoots where I'm looking for half of the spin. The other half it shoots them on the edge of that 180 degree half-circle it will shoot them from.
Answer by QC_Scott · Jul 31, 2014 at 02:08 PM
If you use the transform of the camera instead of the bullet's transform for applying force it should work. A script like this should work.
public GameObject Camera; public GameObject Bullet;
OnMouseDown(){ Bullet.rigidbody.AddForce(Camera.Transform.Forward * 10); }
Then you simply use the inspector to reference the bullet object and camera object. Hope this was helpful.
Hi, I know this is too late, but i have faced the problem today, in case you still need the answer why it only shoots correctly on the half circle, it is because of the "player" collider. If you set the spawn position the same position as the camera, when you look down, the projectile will hit the player collider you can fix this by either disabling collider of projectile or simply move the spawn position a litte bit further in front of the camera, i hope this helps anyone having this problem
Your answer
