Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 JoBoDo · Apr 21, 2014 at 09:30 AM · rotationquaternionspeed

How can I rotate at a constant speed to look at a point?

Hi,

I have to rotate my unit to look at where I click my mouse.

I'm currently using a Quaternion.Slerp function to rotate my unit. However, it takes the same amount of time to rotate 90 degrees, as it does 180 degrees.
If it takes 2 seconds to rotate 180 degrees I only want it to take 1 second to rotate 90 degrees.

Do I have to create logic to change the speed at which I rotate, or is there a better function or something etc to do this?

Thanks for your help :)

Comment
Add comment · Show 1
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 Scribe · Apr 21, 2014 at 11:03 AM 0
Share

You could do the progress divided by the answer you get from Quaternion.Angle(a, b), that would make them the same rate -> you may then need to times it all by a constant say 180 so that 180 degree rotation will stay at the speed it is now but 90 will be reached quicker.

2 Replies

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

Answer by Radivarig · Apr 21, 2014 at 09:42 AM

Try this:

 Plane plane = new Plane(Vector3.up, 0);
 float dist;
 void Update () {
     //cast ray from camera to plane (plane is at ground level, but infinite in space)
     Ray ray = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
     if (plane.Raycast(ray, out dist)) {
         Vector3 point = ray.GetPoint(dist);
 
         //find the vector pointing from our position to the target
         Vector3 direction = (point - transform.position).normalized;
 
         //create the rotation we need to be in to look at the target
         Quaternion lookRotation = Quaternion.LookRotation(direction);
 
         float angle = Quaternion.Angle(transform.rotation, lookRotation);
         float timeToComplete = angle / rotationSpeed;
         float donePercentage = Mathf.Min(1F, Time.deltaTime / timeToComplete);
 
         //rotate towards a direction, but not immediately (rotate a little every frame)
         //The 3rd parameter is a number between 0 and 1, where 0 is the start rotation and 1 is the end rotation
         transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, donePercentage);
     }
 }

The idea is to calculate the rate of changing from 0 to 1 in the last argument of Slerp, since that is the percentage of desired transition, 0 is starting rotation and 1 is rotation we end up with.

from here.

Radivarig

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 JoBoDo · Apr 21, 2014 at 10:29 AM 0
Share

Weird, I thought I replied to this.

Basically, this seems to be what I'm already doing.

Is there some part of your code which means that It will be faster to rotate 90 degrees than it is to rotate 180? I can't see it, I might be missing it.

If there is... I'm missing it, could you please expand?

avatar image Radivarig · Apr 21, 2014 at 11:16 AM 0
Share

Ok, I understand, I thought you wanted it to have the same rotation speed, but you want it to vary as well in different ratio, can you try:

 //add this
 float fixedAngle = 180f;
 ...
 //replace line 17. with this
 float donePercentage = $$anonymous$$athf.$$anonymous$$in(1f, (Time.deltaTime / timeToComplete) * (fixedAngle / angle);
 ...


Set your speed to get 2s for rotating 180 degrees, so if you have 90 degrees it will multiply it with 180f/90f so you get half time because it gets done twice as fast, 270 is 1.5 and same for every other ratio.

avatar image JoBoDo · Apr 21, 2014 at 02:31 PM 1
Share

Awesome! Thanks so much :) That's a really good idea on how to do it! It works perfectly :)

Loving you right now :D

avatar image Radivarig · Apr 21, 2014 at 02:47 PM 0
Share

Glad I could help :)

avatar image
0

Answer by servival · Jun 27, 2015 at 04:01 PM

  using UnityEngine;
 using System.Collections;
 public class AISmoothLookAT : MonoBehaviour {
 public Transform target;
 public Transform looker;
 public Transform Xsmoothlooker;
 public Transform Ysmoothlooker;
 public float Xspeed = 2f;
 public float Yspeed = 2f;
 // Use this for initialization
 void Start () {
 }
 // Update is called once per frame
 void Update () {
 looker.LookAt (target);
 float YRotation = looker.eulerAngles.y;
 float XRotation = looker.eulerAngles.x;
 Xsmoothlooker.rotation = Quaternion.Slerp(Xsmoothlooker.rotation , Quaternion.Euler(0 , YRotation, 0), Time.deltaTime * Xspeed);
 Ysmoothlooker.rotation = Quaternion.Slerp(Ysmoothlooker.rotation , Quaternion.Euler(XRotation , YRotation, 0), Time.deltaTime * Yspeed);
 }
 }
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

23 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

Related Questions

Rotation Ease-In-Out with MAX Rotation Speed 1 Answer

Rotation with different speed for different axis 2 Answers

Smooth rotation about global axis instead of local axis. 1 Answer

Rotation - Simple Question 0 Answers

How can I quickly calculate Land Speed from a Vector3? 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