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 Classic-Niall · May 23, 2013 at 02:26 PM · vector3quaternionquaternion.lookrotation

Quaternion LookRotation

Hi guys - I'm making a tower defence game, yet I've come across a problem.

I have a Trigger Sphere Collider on my turrent, and when the enemy object comes in range, the tower follows and shoots. However, for some reason, the tower rotates in the X axis, which shouldn't happen - it should only rotate on the Y.

Any clues?

 function Update() {
     if (myTarget) {
         if (Time.time >= nextMoveTime) {
             CalculateAimPosition(myTarget.position);
             turretBall.rotation = Quaternion.Lerp(turretBall.rotation, desiredRotation, Time.deltaTime * turnSpeed);
         }
         
         if (Time.time >= nextFireTime) {
             FireProjectile();
         }
     }
 }
 
 function CalculateAimPosition(targetPos : Vector3) {
     var aimPoint = Vector3(targetPos.x + aimError, targetPos.y + aimError, targetPos.z + aimError);
     desiredRotation = Quaternion.LookRotation(aimPoint);
 }
 
 function CalculateAimError() {
     aimError = Random.Range(-errorAmount, errorAmount);
 }
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
0

Answer by robertbu · May 23, 2013 at 02:52 PM

You say "shouldn't", but I don't see any code here that is intended to make that happen. LookRotation() will look at whatever position you specify. The typical way to solve this problem is to bring the y value of the target down to the level of the turret. But you have a second problem here. LookRotation() is looking for a zero based vector, not a position in world space. The code you have here will only work if your turret is at the origin.

So to fix the two problems you would have something like this:

 targetPos = targetPos - transform.position;
 var aimPoint = Vector3(targetPos.x + aimError, transform.position.y, targetPos.z + aimError);
Comment
Add comment · Show 3 · 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 Classic-Niall · May 23, 2013 at 03:05 PM 0
Share

Yea, with your fix, it has the turret ai$$anonymous$$g down, and unable to move back up.

Could it have something to do with the import settings, or the model itself? When I import, I have to change the rotation initially (which I don't understand, but always seems to be the case with 3DS $$anonymous$$ax models).

The Y rotation is fine, but the X also changes, however goes 'flat' (like it is supposed to) when the enemy is directly in the same spot X/Z - position in Y, the top of the dome/turrent moves towards the enemy, flipping vertically (so -Y it goes upside down).

avatar image robertbu · May 23, 2013 at 03:12 PM 0
Share

Yes, initial rotation will make a huge difference. You model needs to have the turret facing positive 'Z' when the rotation is (0,0,0). You can compensate for this in Unity by making the turret a child of an empty game object. And rotating the visible turret to face positive 'Z' when the child has rotation of (0,0,0). The rotation script would then go on the empty parent game object.

avatar image Classic-Niall · May 23, 2013 at 03:44 PM 0
Share

Well that's what I did originally, but playing around with it now, I've noticed that the up direction has become +Z

I may play around with 3DS $$anonymous$$ax and try to re-import it.

EDIT: tried re-importing, same thing - tried empty game object, still rotates down (so on the X axis)

avatar image
0

Answer by Classic-Niall · May 23, 2013 at 04:50 PM

Okay, figured it out.

Instead of Lerd, it is Slerp... and that's it - one letter difference :-D

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

13 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

Related Questions

how to aim an object at another object 1 Answer

Snap a direction vector 1 Answer

How to convert quaternion to vector3 with specific multiplication order? 5 Answers

Help Rotating a Player 0 Answers

How to add 2 Quaternions. 2 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