Search Unity

Need to write my own Vector3.RotateTowards()

Discussion in 'Scripting' started by MicroEyes, Oct 16, 2012.

  1. MicroEyes

    MicroEyes

    Joined:
    Jul 3, 2012
    Posts:
    309
    There is a Inbuilt function in Unity called Vector3.RotateTowards ..

    I tried writing this using CrossProduct then use Axis rotation. but didn't work.

    I am wondering if anybody could write Vector3.RotateTowards for me.

    Thanks for any help.
     
  2. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,102
    Can you just use

    MyTransform.LookAt(target.position);

    Or can you describe better what you are attempting to do?
     
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    So you want a copy implementation of Vector3.RotateTowards(...)?

    Why... it already exists... Vector3.RotateTowards(...).

    If that's NOT what you want, then you need to explain what it is you want. Which you haven't done, because you asked for Vector3.RotateTowards(...).
     
  4. MaarX

    MaarX

    Joined:
    Jul 26, 2012
    Posts:
    39
    Code (csharp):
    1. transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (target.transform.position - transform.position), rotationSpeed * Time.deltaTime);
     
  5. Westerbly

    Westerbly

    Joined:
    Aug 15, 2012
    Posts:
    60
    It can be for practicing purposes, maybe understanding.
     
  6. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    Still requires explanation...
     
  7. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    If he wanted to understand how it worked, he could go read up on rotation matrices, but I doubt the reason he wants to re-write the function is that. More likely OP has a problem that rotatetowards doesnt do what OP is trying to do..
     
  8. Gooren

    Gooren

    Joined:
    Nov 20, 2015
    Posts:
    332
    Hi guys, sorry for the thread necromancy, but... I need the same thing OP did.
    I need a Vector3.RotateTowards alternative that is non-linear for the vector direction change ( something like a Vector3.Slerp(directionA, directionB, Time.deltaTime * angularSpeed * Mathf.Deg2Rad) ).
    Otherwise it's just the thing I need.
     
  9. vc1001

    vc1001

    Joined:
    Feb 8, 2017
    Posts:
    1
    Here it is:
    Code (CSharp):
    1.     Vector3 Vector3RotateTowards(Vector3 current, Vector3 target, float maxRadiansDelta, float maxMagnitudeDelta)
    2.     {
    3.         // replicates Unity Vector3.RotateTowards
    4.         float delta = Vector3.Angle(current, target) * Mathf.Deg2Rad;
    5.         float magDiff = target.magnitude - current.magnitude;
    6.         float sign = Mathf.Sign(magDiff);
    7.         float maxMagDelta = Mathf.Min(maxMagnitudeDelta, Mathf.Abs(magDiff));
    8.         float diff = Mathf.Min(1.0f, maxRadiansDelta / delta);
    9.         return Vector3.SlerpUnclamped(current.normalized, target.normalized, diff) *
    10.         (current.magnitude + maxMagDelta*sign);
    11.     }