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 FlowStateGames · Dec 10, 2012 at 07:12 PM · quaternionmouselooklookrotation

MouseLook script 90 degrees off

I'm using a standard mouselook script to point a cannon object at the mouse cursor. However, the cannon seems to always point 90 degrees to the right or left (depending upon which reference plane I build in the script). I can't figure out why it would do this, so any help would be appreciated.

Here is what I'm seeing from the cannon (red diamond represents the mouse cursor): alt text

I've taken a standard script from here and lightly adapted it. However, there is one key difference between the classic example of this and mine: the plane upon which my items rest is that my plane is on XY, rather than XZ. This is illustrated below: alt text

Finally, here is the code:

 void Update () 
     {
         
         Plane playerPlane = new Plane(Vector3.forward, transform.position);
         Ray hitRay = Camera.main.ScreenPointToRay(Input.mousePosition);
         
         float hitDist = 0.0f;
         
         if(playerPlane.Raycast(hitRay,out hitDist))
         {
             Vector3 targetPoint = hitRay.GetPoint(hitDist);
             
             Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
             transform.rotation = Quaternion.Slerp(transform.rotation,targetRotation, 1); 
         }
     }

Any help would be much appreciated.

cannonrotation.png (9.5 kB)
planeref.png (48.2 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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by sparkzbarca · Dec 10, 2012 at 07:29 PM

the normal for the plane shouldnt be forward if your game lies on the forward plane.

what this code does is create a plane. That plane contains a point and has a normal that define it.

look at rotates from the objects current forward to a new rotation which have the forward pointing towards a point in space.

however since forward is the normal of the plane its going to be wanky.

I THINK you need to change it to vector3.up perhaps to get the right plane.

Appears so.

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 FlowStateGames · Dec 10, 2012 at 07:58 PM 0
Share

I've really hesitated to change the scene to be oriented on the up plane, since I'll have a bunch of changes to make and the idea kinda scares me.

However, if that would simplify this problem, I guess I should suck it up and refactor. Thoughts?

Edit: Just changed it to be Vector3.up, and the object doesn't react at all. It might bear mentioning that I'm using an orthographic camera, so my assumption is that according to my camera, the generated ray is parallel to Vector3.up . I might be wrong, but that's my reading.

avatar image
0

Answer by ScottW · Dec 10, 2012 at 09:31 PM

Is your canon initially aligned to face the z direction (forward)? the blue gizmo arrow should run through the barrel of the cannon towards the barrel mouth.

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 FlowStateGames · Dec 10, 2012 at 09:49 PM 0
Share

Unfortunately, since the orientation of the cannon is completely set by the script, unless I'm misunderstanding you, its orientation in the editor is moot. Again, I'm new to Unity, so if I'm misunderstanding something, my apologies.

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

11 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

Related Questions

Rotating a 2D sprite to face a target on a single axis. 3 Answers

More specific Quaternion.LookRotation 1 Answer

Slowly rotating an object while pointing at mouse 0 Answers

SmoothDamp from 360 to 1 1 Answer

LookRotation Between 2 Points 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