Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Doneyes · Aug 12, 2013 at 11:37 PM · prefabmousebulletshootcurser

How to shoot a bullet to curser position?

Right now the prefab shoots from a game object forward, but I want it to aim it to my mouse's position. How do I do this? Thanks!!

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
2
Best Answer

Answer by robertbu · Aug 13, 2013 at 09:47 PM

Saying aiming at the mouse position, you have to ask "at what distance." If your spawn point is not in alignment with your mouse cursor, then the calculation is a triangulation, and you need the distance in front of the mouse cursor. Start a new scene, put your spawn point back near the camera, and attach this script. Drag your prefab onto the 'prefab' variable in the Inspector. Left mouse click fires:

 #pragma strict
 var prefab : GameObject;
 var distance = 10.0;
 
 function Update () {
     if (Input.GetMouseButtonDown(0)) {
     
         var position = Vector3(Input.mousePosition.x, Input.mousePosition.y, distance);
         position = Camera.main.ScreenToWorldPoint(position);
         var go = Instantiate(prefab, transform.position, Quaternion.identity) as GameObject;
         go.transform.LookAt(position);    
         Debug.Log(position);    
         go.rigidbody.AddForce(go.transform.forward * 1000);
     }
 }

The above script, just set the distance to triangulate at 10. If you have specific targets in the scene, you will want to determine the distance to the target using raycasting. Also note that if you have gravity turned on in the Rigidbody attached to the projectile, it will pull the projectile down and the projectile will hit a bit low.

Comment
Add comment · Show 1 · 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 Doneyes · Aug 14, 2013 at 12:09 AM 0
Share

Thanks it works perfectly. I had to change distance to 50 though.

avatar image
0

Answer by Narv · Aug 13, 2013 at 12:18 AM

You'll want to use Camera.ScreenToWorldPoint(Input.mousePoision)

http://docs.unity3d.com/Documentation/ScriptReference/Camera.ScreenToWorldPoint.html

You'll get the point in the world space that the mouse is pointing at, then you can make your bullet along the path from the camera or spawn point, to that position.

Comment
Add comment · Show 7 · 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 Doneyes · Aug 13, 2013 at 12:32 AM 0
Share

How would I calculate that?

avatar image Narv · Aug 13, 2013 at 12:41 AM 0
Share

http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Lerp.html

spawn at gameobject position

Lerp FRO$$anonymous$$: prefabs current position, TO: Vector returned from ScreenToWorldPoint()

Last variable is time, try with Time.deltaTime and then play around with the value until you get the speed you want.

avatar image Doneyes · Aug 13, 2013 at 01:08 AM 0
Share

But that wouldn't work because if I move my character it calculates the updates differently and wont end up in the right spot. Also I don't think that would let me shoot multiple bullets.

I tried to rotate it to face the mouse point and then shoot forward, but for some reason it spawns and gets stuck.

 var shootlaser : GameObject = Instantiate(laser, transform.position, transform.rotation);
         var screenPos : Vector3 = camera.WorldToScreenPoint (target.position);
         shootlaser.transform.LookAt(screenPos);
         shootlaser.rigidbody.AddForce(shootlaser.transform.forward * force);
avatar image Narv · Aug 13, 2013 at 01:20 AM 0
Share

You are doing it backwards. I linked you ScreenToWorldPoint, not WorldToScreenPoint. You want to go towards the cursor correct? So you are basically "projecting" the mouse cursor from the screen, to where it hits the world.. so you would use Camera.ScreenToWorldPoint(Input.mousePosition) (as mentioned in my original answer)

 var shootlaser : GameObject = Instantiate(laser, transform.position, transform.rotation);
        var screenPos : Vector3 = camera.main.ScreenToWorldPoint(Input.mousePosition);
        shootlaser.transform.LookAt(screenPos);
        shootlaser.rigidbody.AddForce(shootlaser.transform.forward * force);


If you move your character, it should have no affect on the projectile as it's got a rigidbody force on it in a direction (the direction of the mouse projection to the world

avatar image Doneyes · Aug 13, 2013 at 02:04 AM 0
Share

This lerp thing is getting way to confusing for me. The last variable is anyways. I feel like the article threw in a bunch of info that wasn't needed. Also I want it to be smooth while flying and lerping works in frames or something. I feel like making that smooth would cause lag. Here is the line I used anyways:

 shootlaser.transform.position = Vector3.Lerp(transform.position, screenPos, Time.deltaTime);

but honestly it's not worth it to use lerp.

Show more comments

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

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Shoot directions 2 Answers

Have an object move towards the mouse, but... 1 Answer

My object acts wierd, 2 balls being shot instead of one (from different position) 2 Answers

How do i make a fps where the gun always shoots realisticly 3 Answers

Shooting with camera on rails 4 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