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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by Gareth-Elms · Oct 09, 2012 at 09:56 AM · rotationdirectionfollow

Follow target script is rotating only clockwise. Something simple?

Hi,

I've created a scene where a cube follows the mouse cursor :

http://www.garethelms.org/demo/unity3d/follow/WebPlayer.html

A script rotates the cube towards the target and speeds it up the more it is facing the target. I position a yellow sphere to where the cursor is for clarity.

The problem is that the cube only ever rotates clockwise. If you move the cursor so the cube needs a negative angle to face it, it spins a +360 to face it rather than -1. For example if you circle the cursor slowly around the cube in a clockwise direction the cube will keep up and rotate towards it. If you do the same in an anti-clockwise direction the cube spins the wrong way to try and face it.

How can I rotate the cube clockwise and ant-clockwise?

Here is my follow script :

 public class FollowMouse : MonoBehaviour
 {
     public GameObject Cursor; // This is the yellow sphere scripted to follow the mouse cursor
     public float RotationSpeed = 3;
     public float Speed = 10;
 
     void Update()
     {
         var direction = Cursor.transform.position - transform.position;
         var angle = Vector3.Angle( direction, transform.forward);
         transform.Rotate( Vector3.up, angle * (Time.deltaTime * RotationSpeed));
         
         // Divide by angle so that it slows down when facing away from target and speeds up when facing target 
         transform.Translate( Vector3.forward * (Time.deltaTime * (Speed / (angle / 5f))));
     }
 }

Sorry if this is a basic question I'm very new to vectors/quaternions/angles but I'm determined to understand it all.

Thanks Gareth

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

Answer by Mario Jojo · Oct 09, 2012 at 10:22 AM

//Finding the Target Vector:

targetpos = target.transform.position;

//This is my Missile's Direction Forward:

missilevector = transform.forward;

// the direction in which the object (in my case the Ammo/ Rocket) should go:

directionvector = targetpos - transform.position;

// Finding the dot product between the 2 vectors to calculate the angle:

dotproduct = Vector3.Dot(missilevector,directionvector);

// Finding the Angle manually because (Vector3.Angle doesn't give it in minus... this will give an absolute angle:

theta = Mathf.Acos(Mathf.Clamp(dotproduct/(missilevector.magnitude * directionvector.magnitude),0,1));

// This is the basic rotation, movement and all!

rotation1 = transform.rotation;

transform.LookAt(transform.position + directionvector * Time.deltaTime);

rotation2 = transform.rotation;

deltatheta = turningspeed Time.deltaTime;//Mathf.Min(turningspeed Time.deltaTime,theta);

t = deltatheta==0?theta:deltatheta/theta; rotation3 = Quaternion.Slerp(rotation1,rotation2,t);

transform.rotation = rotation3;

speed +=acceleration*Time.deltaTime;

if(speed>=maxvel)

{

speed = maxvel;

}

transform.Translate(Vector3.forward Time.deltaTime speed);

i hope it helps... Main is the angle, you need to describe it well...

Comment
Add comment · Show 7 · 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 Gareth-Elms · Oct 09, 2012 at 11:48 AM 0
Share

Thanks this worked for me. I'm now going to spend time figuring out why it works!

Ultimately I want to reduce the speed while rotating (like a human would have to) so the wider the angle the slower the cube gets. I also want to reduce the rotation speed if it's going fast.

avatar image Gareth-Elms · Oct 09, 2012 at 12:42 PM 0
Share

I achieved slowdown during rotation by using a speed adjuster relative to the distance between the direction the cube is facing and the direction it needs to go :

// This gives a number between -1 and 1 where -1 is 180 // degrees and 1 is 0 degrees var adjuster = 1 - (Quaternion.Angle( rotation1, rotation2) / 90); _speed += (_speed Time.deltaTime) adjuster;

avatar image Mario Jojo · Oct 10, 2012 at 04:37 AM 0
Share

yeah thats nice! $$anonymous$$e was missile, so turning radius was huge!

avatar image Gareth-Elms · Oct 10, 2012 at 12:46 PM 0
Share

$$anonymous$$ario how did you learn about vectors and calculating angles between vectors etc? I've spent hours on it with little progress is this normal!?

avatar image Mario Jojo · Oct 11, 2012 at 05:07 AM 0
Share

haha Gareth, i used to suck at these things too about a month ago! then i joined this company IndusGeeks, and the lead programmer $$anonymous$$ches me everything! this was his idea to calculate everything using $$anonymous$$aths, and doesnt allow me to cheat with tweaks and all! so its kinda fun experimenting with maths!

Show more comments
avatar image
1

Answer by shaderop · Oct 09, 2012 at 10:12 AM

You might want to use Quaternion.LookRotation to get the rotation that you'll need to look at the cursor and then use Quaternion.Slerp to interpolate between the object's current rotation and the desired look at rotation.

But I think the problem you have now is that while Vector3.Angle will give you the smallest angle between two vectors, you still need to specify the direction of the rotation, i.e. whether the angle is positive or negative.

You can do that by testing for the dot product:

 if (Vector3.Dot(direction, transform.forward) >= 0)
    transform.Rotate( Vector3.up, angle * (Time.deltaTime * RotationSpeed));
 else
    transform.Rotate( Vector3.up, -angle * (Time.deltaTime * RotationSpeed));

You might want need to switch change the comparison from >= to <= if the cube rotates in the wrong directon.

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 Gareth-Elms · Oct 09, 2012 at 11:43 AM 0
Share

For some reason the cube stuck when using -angle, I was working on it when $$anonymous$$ario answered too and his solution worked

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

10 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

Related Questions

Rotate spaceship based on mouse movement 2 Answers

Tree Fall Direction 1 Answer

Straighten out camera rotation 1 Answer

Shoot an object and have it move based on rotation 1 Answer

Getting the face direction 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