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 Wixelt · Jul 23, 2019 at 08:39 PM · rotation3dmouse

How to make in-game weapon aim at location of mouse?

I have a system where the weapon on the player object is meant to be aimable seperately to the actual movement of the object, though still within what's currently invisible on screen, aiming at wherever the mouse currently is.

As it stands, though, it only points relative forward, and stops following the rotation of the player entirely when I try certain things. Does anyone have a good method of making the weapon object rotate/aim based on the position of the mouse? As it stands i've been trying a mix of Quaternion.RotateTowards and Camera.main.ScreenToViewportPoint(Input.mousePosition), but this isn't working (quick thought: Do I need the UI code block for this to function?).

Full section of code below:

 void Update()
     {
         //shotLoad is the script name for the weapon object, where projectiles are being launched from, just in front of the player.
         shotLoad.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(Camera.main.ScreenToViewportPoint(Input.mousePosition)), Time.deltaTime * 10);
     }
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 I_Am_Err00r · Jul 23, 2019 at 08:47 PM

 public class ExampleClass : MonoBehaviour
 {    
     // Angular speed in radians per sec.
     float speed;
 
     void Update()
     {
         Vector3 targetDir = Camera.main.ScreenToViewportPoint(Input.mousePosition) - transform.position;
 
         // The step size is equal to speed times frame time.
         float step = speed * Time.deltaTime;
 
         Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, step, 0.0f);
         Debug.DrawRay(transform.position, newDir, Color.red);
 
         // Move our position a step closer to the target.
         transform.rotation = Quaternion.LookRotation(newDir);
     }
 }

Copied directly from here, slightly edited to what I think you are trying to do. Don't have a compiler to test, so let me know if that works.

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 Wixelt · Jul 23, 2019 at 09:13 PM 0
Share

This is much closer than i've been so far, so thanks. :)

Only issue now is that whilst the mouse is affecting the direction of the shots, they're all going off into somewhere in the mid to upper left area of the screen, to varying degrees based on mouse location.

avatar image I_Am_Err00r Wixelt · Jul 23, 2019 at 09:21 PM 0
Share

I don't know how you project your bullets, but it has something to do with that script, change the projectile function to something like this:

 bullet.transform.position = Vector3.$$anonymous$$oveTowards(bullet.transform.position, targetDir, Time.deltaTime);
 
avatar image Wixelt I_Am_Err00r · Jul 23, 2019 at 09:43 PM 0
Share

The movement projection I was using was in a separate script (something i've accommodated for in your suggestion), attached to the projectile instances themselves, and relied on velocity:

rb.velocity = transform.TransformDirection(Vector3.forward * speed);

This never gave me direction for the shots, though the issue now is that what you've suggested may or may not be giving me direct for shots, but I can't tell because the bullets aren't moving. I've assumed this had something to do with how it's interpreting Time.deltaTime, but I can't be sure.

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

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

Rotate a crank with the mouse 0 Answers

Mouse axes based on position, not movement. 1 Answer

Player mouse rotation 2 Answers

3D Turret Rotation 0 Answers

How to rotate an object by its pivot in script 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