Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

RotateTowards Confusion

Discussion in 'Scripting' started by shotoutgames, Feb 27, 2017.

  1. shotoutgames

    shotoutgames

    Joined:
    Dec 29, 2013
    Posts:
    290
    Hi all.
    I was trying to create a script to use the right stick to rotate around my player (follow)
    I played around with RotateAround. I can't get the camera to orbit the player no matter how much I strip the code (See below).
    I'm not looking for an alternate / better method. I'm just trying to understand why the following will not rotate the camera around the target but just seems to spin around itself with the transform position remaining static.
    I know the transform.position and rotatetowards are "fighting" each other. But even with the left stick still it will not rotate the camera around the target object.

    Thanks for any input.

    Code (CSharp):
    1.  void LateUpdate()
    2.     {
    3.         RightStick();
    4.         Vector3 follow = new Vector3(followXForm.position.x, followXForm.position.y, followXForm.position.z);
    5.         Vector3 offset = new Vector3(0, yDistance, -zDistance);
    6.         Vector3 target = follow + offset;
    7.         float xD = Mathf.Abs(transform.position.x - target.x);
    8.         if (xD >= xDistance)
    9.         {
    10.             Vector3 ps = new Vector3(target.x, target.y, target.z);
    11.             transform.position = Vector3.Slerp(transform.position, ps, Time.deltaTime * smoothMovement);
    12.         }
    13.  
    14.     }
    15.  
    16.      void RightStick()
    17.     {
    18.         // right stick camera
    19. //different code before but testing rotatearound
    20.         transform.RotateAround(follow, Vector3.up, smoothRotate * Time.deltaTime);
    21.  
    22.     }
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    What is smoothRotate? Where and how is that set?
     
  3. shotoutgames

    shotoutgames

    Joined:
    Dec 29, 2013
    Posts:
    290
    Thanks.
    Just inspector damping values.
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
     
  5. shotoutgames

    shotoutgames

    Joined:
    Dec 29, 2013
    Posts:
    290
    Say what :) ?
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    What is the value of smoothRotate. Give me a number. 0? 0.5? 1? 10? 1000?
     
  7. shotoutgames

    shotoutgames

    Joined:
    Dec 29, 2013
    Posts:
    290
    I see now that lateupdate just resets the transform.position each frame. cancelling out changes to rotatetowards transform change. Don't know how to rotate around object while still keeping this the same though.

    Smoothrotate is 60 right now so it just slerps quickly. Changing the value has no affect.

    Sorry. Thanks
     
  8. shotoutgames

    shotoutgames

    Joined:
    Dec 29, 2013
    Posts:
    290
    I added a condition to not allow transform to change in lateupdate if leftstick is pressed. It now rotates around but still questions.
    It rotates at a point the zdistance away from the target in the world. If I set it to rotate around the target. How can I get it to orbit a set zDistance away from the target location all the way around?
    As it stands now with Z at 40 it rotates at distance Z in the world to the target.