Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by dspirelja · Apr 18, 2017 at 01:52 PM · instantiateaddforcescreentoworldpoint

Need help Instantiating a projectile and adding a force to it, from player position,towards mouse position

 public Transform gun;
 public Rigidbody bullet;
 void Update()
 {        
     if (Input.GetMouseButtonDown(0))
         Fire();
 }


 void Fire()
 {
     Vector3 mousePosit = Camera.main.ScreenToWorldPoint(Input.mousePosition);
     //mousePosit.y = 19;
     Rigidbody firedProj;
     firedProj = Instantiate(bullet, gun.position, gun.rotation);
     firedProj.AddForce(mousePosit.normalized * 5000);
 }

This is the code I am using currently.The outcome of this is that the projectiles are shot towards the camera position not the mouse position.I am using an RTS bird view camera which you can move by scrolling to the edges with your mouse. So I don't understand why I don't get the mouse position in world space.

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

Answer by Matthewj866 · Apr 18, 2017 at 05:32 PM

@dspirelja Hi there, and welcome to Unity answers!

This seems to be a bit of a strange way of achieving what you want - why not just make the gun look at the target by using Transform.LookAt() then applying force to the new bullet along one axis? Specifically, the axis now pointing at the object?

Documentation: https://docs.unity3d.com/ScriptReference/Transform.LookAt.html

Example:

  void Fire()
  {
      Vector3 mousePosit = Camera.main.ScreenToWorldPoint(Input.mousePosition);
      gun.LookAt(mousePosit);
      Vector3 forceToAdd = new Vector3(0f, 0f, 0f); //change these values to whatever you need to make the bullet move along one specific axis
      GameObject firedProj;
      firedProj = Instantiate(bullet, gun.position, gun.rotation) as GameObject;
      firedProj.GetComponent<Rigidbody>().AddForce(forceToAdd);
  }

please note this will only apply the force once, so if you need to apply the force consistently you will need to adjust the code to do that.

Hope this helps!

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 dspirelja · Apr 19, 2017 at 09:16 AM 0
Share

I need a little bit more help, the gun is supposed to rotate to the mouse position?In my case it always rotates to the same position ignoring the mouse position.Could you also help me rotate the player to the same direction where the projectile is shot?

avatar image Matthewj866 dspirelja · Apr 19, 2017 at 09:25 PM 0
Share

It will never rotate if you are not constantly calling LookAt() in Update() and will only update when Fire() is called.

You can just adapt what I have mentioned above to get the desired effect for the player.

If you run into additional issues when adding this functionality for the player I recommend opening another question.

If my answer solved your problem, please mark it as the answer.

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

112 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 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 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 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 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 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 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

How to move an Instantiated Object towards a moving Object? 0 Answers

How can i make an object move to my aiming position? 0 Answers

Addforce is not working 1 Answer

[Unity2D] Firing bullets based on gun orientation 1 Answer

Can't apply AddForce after Instantiating object 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