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 samersamy · Aug 18, 2011 at 12:51 PM · mousepositionaim

Mouse aims at object ?

Hi all,

How to map mouse position on screen to world position ?

I know that this question has been asked a lot, but i have a problem.

In my game i display a crosshair instead of the mouse cursor for the player to aim. When "Fire" is clicked i want to instantiate a sphere(the missile i use) at a position relative to the cursor's position.

I tried Raycast, but it don work as i want it to be. This is almost close to what i want to achieve.

I use the following code:

     // get the position of the mouse
     var mousePos : Vector3 = Input.mousePosition;
     var mouseToWorld : Vector3 = Camera.main.ScreenToWorldPoint (Vector3(mousePos.x , mousePos.y, Camera.main.nearClipPlane));
     // instantiate a new object of the type of the missile
     var instantiatedProjectTile : Rigidbody = Instantiate(projectTile, mouseToWorld, Quaternion.identity);
     // adjust the velocity of the missle in the Z direction
     instantiatedProjectTile.velocity = transform.TransformDirection(Vector3(0, 0, speed));


But it don work properly, the spheres don go in the direction of the object the cursor was aiming at.

P.S: I am using a Terrain as my playground.

Any help is appreciated,

Thanks,

Samer

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Joshua · Aug 18, 2011 at 02:50 PM

When you instantiate them you want them not to give a rotation of Quaternion.identity but the rotation of the camera±

 Instantiate( ..., ..., Camera.main.transform.rotation);

Then the velocity you want to give it is simply a local forward one

 ...velocity = instantiatedProjectile.transform.forward * magnitude

where magnitude is the magnitude of the velocity you want to give it.

Comment
Add comment · Show 2 · 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 samersamy · Aug 18, 2011 at 02:59 PM 0
Share

how can i multiply the Rigidbody by the magnitude !!! "...velocity = instantiatedProjectile * magnitude "

avatar image Joshua · Aug 18, 2011 at 03:02 PM 0
Share

Hah, I'm sorry, seems I forgot to write the final bit. It's the rigidbody's transform's forward vector you want. ;)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Rotate player to aim on one axis (Z-axis) towards mouse position/joystick - 2.5D (3D) 0 Answers

Cast a cursor into world space from a game object? 0 Answers

Make my Gunpoint aim/rotate on the z axis towards my mouse cursor 1 Answer

While holding down the mouse button make object rotate to mouse cursor 1 Answer

Top down aim at mouse 3 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