Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 danik550 · Aug 30, 2019 at 11:52 PM · rotationmoverotate objectmovingrotatetowards

Vector3.RotateTowards Problem

Could someone explain why the code from Unity's website regarding Vector3.RotateTowards does not work?

  1. https://docs.unity3d.com/ScriptReference/Vector3.RotateTowards.html

public class ExampleClass : MonoBehaviour

{

 // The target marker.
 Transform target;

 // Angular speed in radians per sec.
 float speed;

 void Update()
 {
     Vector3 targetDir = target.position - transform.position;

     // The step size is equal to speed times frame time.
     float step = speed * Time.deltaTime;

     Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, step, 0.0f);
     Debug.DrawRay(transform.position, newDir, Color.red);

     // Move our position a step closer to the target.
     transform.rotation = Quaternion.LookRotation(newDir);
 }

}

Even a simple box wont rotate what so ever. I really need something like this for my game and even this example will not work.

Could someone explain why or even post a fix or an alternative?

Comment
Add comment · Show 2
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 Bonfire-Boy · Sep 10, 2019 at 11:39 AM 0
Share

If you're trying to use that code unmodified then seems to me there's an obvious issue in that speed is private and not assigned to, so will have the default value of zero. I would suggest a) making it public so you can tweak it and b) setting its default value to 1.0.

avatar image Bonfire-Boy Bonfire-Boy · Sep 10, 2019 at 11:41 AM 0
Share

Having said that, target is also private and not assigned to. So presumably you're not using the code unmodified as it'd be throwing null reference exceptions.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Lawrence57 · Sep 09, 2019 at 09:12 AM

I'm trying to achieve specialized movement in a circular direction around a point. For example, the character moves from in front of the point to the right of the point while maintaining their distance from the point. After some struggling with complicated math, I got to thinking I might be able to use Vector3.RotateTowards with the angleRadiansDelta calculated.

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 DCordoba · Sep 10, 2019 at 11:29 AM 0
Share

hello if you objetc that rotate have a transform you can use Transform.RotateAround and you have solved the problem

Otherwise you need a right, forward, and up vector (if you have just 2, example forward and up, like 3 point plane, you can use Vector3.Cross to get the right)

taking the current angle you can use sine and cosine to get the position on (x,y) relative to the angle

suppose to you will store the desired position in currPos (current position) variable, point to pivot in centerPoint and distance to the point in distance, lets draw it like a vector on a 2d plane formed by the point center (as the 0 on the plane), the right(as the x axis) and forward (as the y axis)

to see the position (x, y) of certain rotation + distance to center on a plane, you need to perform the next calculus, source

(cosine(angle), sine (angle))*distance to center

to do this on 3d plane using forward and right vectors you just have to multiply sine, and cosine results (`float`), by the the axis vectors normalized (`Vector3`) and the multiply the result to the distance, more clearly I think expressed in code

 //initialize var
 Vector3 currPos = Vector3.zero;
 
 //get the position on x axis (cosine) and multiply it by right axis 
 //(right direction to the plane on 3d space where the circle rotate)
 currPos = right.normalized * $$anonymous$$athf.Cos(angle);
 
 //get the position on y axis (sine) and multiply it to forward axis and sum to the previous
 currPos += forward.normalized * $$anonymous$$athf.sin(angle);
 
 //mutiply that by the desired distance around the point
 currPos *= distance;
 
 // translate de vector to the center, adding it
 currPos += centerPoint;

so, how to get forward and right vectors? if you are rotating around a transform, use the native forward and right (or up if you need)

if you have a axis that you define the normal to the plane where the "circle of rotation" will be "drawed" you can use cross product to get two perpendiculars easy

 Vector3 nonParallelVector = axis;
 Vector3 right:
 Vector3 forward;
 
 //setting a non parallel vector of axis, to do a croos product and get first perpendicular
 if($$anonymous$$athf.Approximately(axis.x, 0)) //x == 0 using float...
      nonParallelVector.x = 1;
 else (){
      nonParallelVector.y = nonParalelVector.x;
      nonParallelVector.x = 0;
 }
 
 right = Vector3.Cross(nonParallelVector, axis); //get perpendicular to two non paralel vectors
 forward = Vector3.Cross(right, axis); //get the last perpendicular to the axis vectors

if you need a random forward and right, you can take two different points, get the vectors from center and calculate two cross products, etc

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

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

Related Questions

Having trouble rotating a turret 1 Answer

how to rotate a gameobject in a given direction and stop when it's done 1 Answer

how to tell a game object to move between 2 points(A,B) along x axis , right to left, and then move left to right back to its first position ??? 3 Answers

RotateTowards in a specified direction 0 Answers

Quaternion.RotateTowards() immediately snaps to target rotation 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