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
0
Question by Tetheta · Feb 09, 2014 at 06:10 AM · rotate objectsidescrollermouse click

Unity2D problem with negative z rotation shooting bullet towards mouse click

I have what seems to me to be a very odd error, though I'm sure it's something simple I'm missing here. I'm rather new to Unity, and as such most of my code at the moment is adopted from tutorials.

I have the following code to shoot an object from my player's gun at where a user clicks with their mouse. The odd thing is that if I click right next to the gun, the bullet displays fine. But if I click even a small distance away the bullet rotates sideways in the z axis and is very difficult to see in the 2D view.

The bullet is fine if Z is 0 (which it is when clicking close to the player) but Z becomes progressively more negative negative the farther you click (10^-7 a short distance away and still only partially rotated, then 10^-6, then farther than that -- now almost invisible in the 2D view).

I am going to be reading up on Quaternions and the like as I do not understand how Unity uses them exactly yet but if anyone could point me in the right direction it would be much appreciated.

 public GameObject GravityLiftShot;
     private Vector3 mousePos;
     private Vector3 objectPos;
     private float angle;
     private float shotSpeed = 1000;
 
     void FixedUpdate()
     {
         //Point the gun at the mouse
         mousePos = Input.mousePosition;
         mousePos.z = 0;
         objectPos = Camera.main.WorldToScreenPoint(transform.position);
         mousePos.x = mousePos.x - objectPos.x;
         mousePos.y = mousePos.y - objectPos.y;
         angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg - 90;
         Vector3 rotationVector = new Vector3(0, 0, angle);
         transform.rotation = Quaternion.Euler(rotationVector);
 
         //Fire!
         if (Input.GetMouseButtonDown(0))
         {
             GameObject shot = (GameObject)Instantiate(GravityLiftShot, transform.position, transform.rotation);
             shot.transform.LookAt(mousePos);
             shot.rigidbody2D.AddForce(shot.transform.forward * shotSpeed);
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 robertbu · Feb 09, 2014 at 06:38 AM

There are a number of potential issues here. Since I don't know how your game is setup, it is hard to figure out which ones are impacting your game. The top issue is line 23. LookAt() takes a position in 3D space. You are passing it a screen direction. You can solve it by:

 shot.transform.rotation = Quaternion.LookRotation(mousePos);

Note this code is only going to work if 'GravityLiftShot' is a 3D object with the front of the object looking at positive 'z' when the rotation is (0,0,0). If 'GravityLiftShot' is a sprite you will need different code.

Another issue is the 'z' value of mousePos. Assuming all of your 2D action is happening at 'z' = 0, then you need to set it to '0'. Alternately you can set 'z' on line 11 to the distance in front of the camera. So if the camera is at z = -10, and the object is a z = 0, do on line 11:

 mousePos.z = 10; 
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 Tetheta · Feb 22, 2014 at 02:46 AM 0
Share

Sorry for the much delayed response (had a lot of things suddenly go on and wasn't able to work on this) but yes that was indeed my issue (I was trying to use the functions on a sprite).

I was able to find someone doing something similar with Javascript and use their code ins$$anonymous$$d which was a bit simpler (as expected since I'm messing with 2D only) many thanks for the help! I'd thumbs up but I'm not active enough yet =/

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

18 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

Related Questions

How to make an object roate 180 on Z axis on each mouse click 1 Answer

Copy rotation values in between objects. 1 Answer

how to rotate cube smoothly 1 Answer

How can I lock the rotation angle at a certain point? 0 Answers

Weird shaky player behavior (moving through colliders) 0 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