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
3
Question by Ziad · Aug 12, 2011 at 10:41 PM · fpsprojectileaiming

making the weapon shoot whatever the crosshair is on

i'm making FPS Game and i want the projectile to go at the center of the screen........ no mater how far or how near is the object that i'm looking at

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

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by aldonaletto · Aug 13, 2011 at 12:34 AM

You can use ViewportPointToRay to create a ray from the screen center and get the hit point as your target, then get the direction from the vector (hit.point - spawnPoint.position):
EDITED: Altered to shoot even when nothing was hit by the raycast - it uses the ray direction in this case:

var rocketPrefab: Transform; // your rocket prefab var rocketSpeed: float = 20; // rocket speed var spawnPoint: Transform; // empty object which defines the spawn point

function Update () { if (Input.GetButtonDown("Fire1")){ // create a ray from the center of the screen var ray: Ray = Camera.main.ViewportPointToRay(Vector3(0.5,0.5,0)); var hit: RaycastHit; var dir: Vector3; if (Physics.Raycast(ray, hit)){ // if something aimed // find the direction from spawnPoint to the target dir = (hit.point - spawnPoint.position).normalized; } else { // if there's nothing, use the ray direction dir = ray.direction; // spawnPoint.forward works too (see note below) } // ensure the rocket will born pointing to the target var rot = Quaternion.FromToRotation(rocketPrefab.forward, dir); // instantiate the rocket var rocket = Instantiate(rocketPrefab, spawnPoint.position, rot); // launch the rocket rocket.rigidbody.velocity = dir rocketSpeed; } } NOTE:* An alternative could be to use the spawnPoint.forward direction instead of ray.direction. This makes little difference unless the spawPoint is much lower than the camera.

Comment
Add comment · Show 3 · 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 Ziad · Aug 13, 2011 at 01:37 AM 0
Share

O$$anonymous$$G Thats cool thnx alot......... but there is something about it ... i want to shoot even if i'm not looking at anything cuz this script making me shoot only if i'm looking at something

avatar image Ziad · Aug 13, 2011 at 01:52 AM 0
Share

Please....

avatar image aldonaletto · Aug 13, 2011 at 02:53 PM 2
Share

Yes, this script only shoots if there is something aimed because it needs the hit.point. But you can use ray.direction in other cases. I edited my answer to include this case.

avatar image
0

Answer by Bosstvgames · Mar 17, 2014 at 01:46 AM

The script works GREAT! but I have one question I`m making a fps game so I would like to have the gun shoot straight at the cursor or at the cross hair its set at the middle of the screen so please help me if can!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

UFPS Projectile Spawn Point (SOLVED) 2 Answers

Multiplayer projectiles 0 Answers

How can I achieve precise projectile shooting towards the crosshair in Unity? 1 Answer

How to speed up a trigger enter function? 1 Answer

Rotate RayCast/Object right on instantiation? 1 Answer


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