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
1
Question by suttsc · Jul 18, 2012 at 02:24 PM · quaternionlerpaxisanglelock

Rotating an object around Z axis using Lerp

I have spent a few hours going through different pieces of code and have been unable to find anything that limits the rotation to just one axis.

This is what I have so far... explanation to follow.

 void Update(){
 if(gotTarget)
 {
     i += (speed * Time.deltaTime);
     transform.rotation = Quaternion.Slerp(start, target, i);

     if (transform.rotation == target)
     {
         i = 0f;
         gotTarget = false;
         fireMissile();
     }
 }

}

public void giveTarget(Vector3 pointIn){ if(!gotTarget)

{

gotTarget = true; start = transform.rotation;

     Ray r = Camera.main.ScreenPointToRay(pointIn);
     point = r.origin;
     point.z = transform.position.z;

     target = Quaternion.LookRotation(point - transform.position);
 }

}

the pointIn is mousePosition. transform.rotation == target is not working 100% at the moment.

the idea is when the object (a turret) is given an input, it will rotate around the Z to face the input position with its top (of a cylinder) smoothly using Lerp. when it reaches its final rotation, it will fire, then be ready for the next input.

however, the turret seems to be rotating through all angles in a peculiar way until.

i have uploaded a version to dropbox for the rotation to be seen. and you may see it not fire everytime as an if statement doesn't always work. https://dl.dropbox.com/u/34478950/Game/test/WebPlayer/WebPlayer.html

any help appreciated

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
3
Best Answer

Answer by suttsc · Jul 19, 2012 at 10:29 AM

I found a solution to my problem on this forum page. http://forum.unity3d.com/threads/36377-transform.LookAt-or-Quaternion.LookRotation-on-1-axis-only Just mentioning for other people searching for an answer to a similar problem.

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

Answer by whydoidoit · Jul 18, 2012 at 04:58 PM

Just use a Mathf.Lerp and only apply it to the Z axis:

    var r = transform.eulerAngles;
    transform.rotation = Quaternion.Euler(r.x, r.y, Mathf.LerpAngle(start, target, i));
Comment
Add comment · Show 4 · 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 Matt-Downey · Jul 18, 2012 at 07:02 PM 2
Share

use LerpAngle! http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$athf.LerpAngle.html

avatar image suttsc · Jul 18, 2012 at 08:37 PM 0
Share

start and target are quaternions so invalid arguments in $$anonymous$$athf.Lerp/LerpAngle I'll give it a go using start and target as floats from the z's i can obtain. edit: Nope that just buggered it up

avatar image Bovine · Jul 18, 2012 at 09:59 PM 0
Share

You probably want to Slerp then...

http://docs.unity3d.com/Documentation/ScriptReference/Quaternion.Slerp.html

avatar image suttsc · Jul 19, 2012 at 08:18 AM 0
Share

Already tried slerp, unless you meant ins$$anonymous$$d of $$anonymous$$athf.Lerp, but it returns a Quaternion which would fit.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Creating a multiple part turret what locks onto certain axis. 4 Answers

Rotation Lerp 2 Answers

Quaternion LookRotation/Slerp Axis Lock 3 Answers

90 Degree stopping rotation on y-axis issue 0 Answers

How to Increase Height of Pendulum? 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