how do i make it so the bullet always goes straight towards the middle of the crosshair even if the bullet isnt at the middle of the screen?
3 Answers
3Ok... If you have a gun, say like on the side of the screen like in a FPS, you realize that even though it shoots strait into space and the bullet ends up at the center of the screen, it doesn't work when the thing you are shooting is closer. I think this is your question, but I am not sure.
To solve this, try making a raycast from the center of the screen (or where your crosshair is) to the 3d environment. Make it very long, but not infinite. Then make the spawning object at the tip of the gun and make it always face where the raycast hits a object. The reason for making the raycast really long is because if it doesn't hit a object, you must make the spawner always shoot to the endpoint of the ray, giving the effect of shooting directly in the center of the screen. I have not tested this method, but in theory, it works.
EDIT:
If you don't know how to cast a ray, and gain information from the hit, look at the unity scripting reference on raycasting here. If you know how to do that and just don't know how to cast from the center of the screen, use this:
var ray : Ray = camera.ScreenPointToRay (Vector3(512,384,0));
if(Physics.Raycast(ray,hit,100000.0)){
//set the spawn object to aim here
}
That Vector3(512,384,0) is the screen coordinates for the center of a Standalone(1024,768) screen only, you will have to change those values if the game screen is not that size. Also, if your gun shoots stuff that are invisible, like bullets, you don't need a spawner, or even a gun. Just make a raycast from the center of the screen when you click. If it hits a enemy, make the enemy loose health, for example. The large ray length is for the "shoot into space" effect, when your ray hits nothing, and you still shoot, then set the spawner to aim at the endpoint.
I hope this answers your question.
You could do a raycast first from camera straight forward then adjust the gun aim to point at the raycast hit with a default to a certain range if there is no hit.
hey im have a similar problem, well i have my gun and the spwan point for the bullit right in front of the nozzle (tip) and it shoots fine except if you watch the bullit travel it flys and then is almost pulled to the floor almost as if the bullet is magnetically attracted to the floor, so if i aim up woods the bullet flys straight for the ground. ANy thoughts ???
Please post a new question, instead of posting useless copypasta 'answers' to different questions.
– syclamoth
repeating answer, of what I said above^. But maybe it is more clear and helpful.
– Meater6I didn't refresh the page before posting. Yours wasn't there at the time for me. :)
– anon9147783http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html?from=RaycastHit
– anon9147783I thought of something, what if you move right next to a wall? Your gun might be parallel to the wall, to compensate for the close range. That might look awkward.
– Meater6If you are that close you might shoot yourself in the chin too. :) Of course you also can't see anything but wall.
– anon9147783