Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by P48l0 · Feb 25, 2010 at 06:42 PM · fpsaiming

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 );

   }

 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges