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 /
  • Help Room /
avatar image
0
Question by gusbmurphy · Oct 09, 2019 at 03:11 PM · rotationtransformlookat

Trouble with Rotations and Transform.LookAt() (I think!)

Hi everyone! Sorry that this is a crosspost from the Unity3D Reddit, but it hasn't got much (any) traction there. Since there isn't a very elegant way to share video here (that I know of!), you can see the problem here on the Reddit thread—sorry again if that's bad manners to just link to over there! This is my first Unity project outside of a couple of tutorials, and I'm making an isometric space shooter with WASD and mouse controls. I'm running into an issue with my "aiming" system. There's a reticle that's moved with the mouse, that the player's ship (white) is made to rotate towards, essentially with the Transform.LookAt() function, meanwhile the movement is controlled with RigidBody.AddForce(). These both work well, but as soon as the ship is moved and there is rotation, the ship no longer fires it's projectiles correctly. You can see in the included video that projectiles are fired in the direction I would expect, but as soon as the ship is moved, something goes wrong and the projectiles are fired in all sorts of directions that don't seem to make sense. Here's the function (called from LateUpdate() in the script on the reticle) that rotates the ship:

 private void AdjustPlayerAim()
     {
         player.transform.LookAt(aim.transform);
     }

And here's the relevant movement code in the script living on the player's ship (CheckForInput() is called in Update()):

 private void CheckForInput()
     {
         if (Input.GetButtonDown("Fire1"))
         {
             InvokeRepeating("Fire", 0f, 60f / fireRate);
         }
         else if (Input.GetButtonUp("Fire1"))
         {
             CancelInvoke();
         }
 
         if (Input.GetKey(KeyCode.A)) { Thrust(cameraLeft); print("Thrusting left"); }
         if (Input.GetKey(KeyCode.D)) { Thrust(cameraRight); }
         if (Input.GetKey(KeyCode.W)) { Thrust(cameraForward); }
         if (Input.GetKey(KeyCode.S)) { Thrust(cameraBack); }
     }
 
 public void Thrust(Vector3 direction)
     {
         shipRigidbody.AddForce(direction * thrustForce * Time.deltaTime);
     }


What I'm observing is that as soon as the ship moves, the game object where the projectiles are instantiated from gets its rotation completely screwed up. As you can see in the video, if I'm just staying still and rotating, everything works as I'd expect, but as soon as the ship moves it goes south. Here's an image of the rotation in the editor (with the mentioned game object selected):

alt text

What's frustrating is that when I have Local turned on in the editor, the rotation looks good! What's going on? Thanks again for your attention!

screen-shot-2019-10-08-at-103948-am.png (141.0 kB)
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 gusbmurphy · Oct 09, 2019 at 03:53 PM

Well, I solved it! Very silly mistake where I was giving the projectiles a velocity from a unit vector that didn't make any sense, just had to straighten that part out.

Comment
Add comment · 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

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

239 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Unity 2D lookat and shoot issues 0 Answers

Creating a Window - Camera movement 0 Answers

transform.lookat is not working correctly 0 Answers

LookAt() rotating the wrong way 0 Answers

i'm using a code to make my cube walk with character controller but when i play the cube spins. 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