- Home /
Shoot where to camera direction ?
I did the FPS tutorial but when i shoot the projectiles take off from the weapon to the direction where the weapon is looking. I want thet the direction be the direction where the user is aiming. I did this But donset work:
function Update() {
if( Input.GetButtonDown( "Fire1" ) )
{
var ray = new Ray (cam.transform.position,cam.transform.forward);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 100)) {
Debug.DrawLine (transform.position, hit.point, Color.red);
Debug.log(hit.collider);
var instantiatedProjectile : Rigidbody = Instantiate(
projectile, transform.position, transform.rotation );
transform.LookAt(hit.point);
instantiatedProjectile.velocity =
transform.TransformDirection( new Vector3(0,0,speed));
Physics.IgnoreCollision( instantiatedProjectile. collider,
transform.collider );
}
}
}
Answer by Shawn · Feb 25, 2010 at 06:49 PM
Well I assume you are trying to shoot targets which are at your reticle's screen location. To do this you would want to get the screen location of your reticle (or the center of the screen if you don't have a reticle and just want to use the center of your screen) and then use the cam.ScreenPointToRay method to get the ray. Use that ray to look for your targets and it should work.
Answer by e-bonneville · Mar 25, 2010 at 08:51 PM
All you have to do is go into the first person controller on your scene panel, open it up, and drag your gun script onto the main camera. If you drag it onto the First Person Controller alone, it just points where the controller is pointed. If you drag it onto the camera, it will fire where the camera is looking at the time. Try it!
Answer by Random Indie · Apr 23, 2010 at 06:26 AM
Check out the 3rd Person Shooter Example. It does pretty much exactly what you're looking for.
IIRC the pertinent game objects are the player, AimTarget, MainCamera, and the Third person camera controller script (can't remember it's name exactly but it's attached to the camera).
With some fenagaling you should be able to sort it out. It's like what shaKu suggested.
Your answer
Follow this Question
Related Questions
making the weapon shoot whatever the crosshair is on 2 Answers
FPS Deadzone aim 0 Answers
Fps Aiming Script help 2 Answers
Best way for setting up shooting mechanics for an FPS? (iPhone) 1 Answer
FPS aim is slightly off center - using ScreenPointToRay 2 Answers