Search Unity

  1. Engage, network and learn at Unite Austin 2017, Oct 3 - 5. Get your ticket today!
    Dismiss Notice
  2. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  3. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  4. Unity 2017.1 is now released.
    Dismiss Notice
  5. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  6. Unity 2017.2 beta is now available for download.
    Dismiss Notice

Rotate camera arround vector from spherical surface to player?

Discussion in 'Scripting' started by Vitaly-Abilevich, Sep 13, 2017.

  1. Vitaly-Abilevich

    Vitaly-Abilevich

    Joined:
    Nov 6, 2013
    Posts:
    47
    Good day community!

    I got this code attached to a camera:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class CameraController: MonoBehaviour {
    4.  
    5.     public Transform target;
    6.     public float distance = 5f;
    7.     public float xSpeed = 10f;
    8.     public float ySpeed = 10f;
    9.     public float scrollSpeed = 20f;
    10.  
    11.     float yMinLimit = -80f;
    12.     float yMaxLimit = 80f;
    13.  
    14.     float distanceMin = 5f;
    15.     float distanceMax = 50f;
    16.  
    17.     float x = 0f;
    18.     float y = 0f;
    19.     float s = 0f;
    20.  
    21.     float smoothTime = 0.2f;
    22.  
    23.     float xSmooth = 0f;
    24.     float ySmooth = 0f;
    25.     float xVelocity = 0f;
    26.     float yVelocity = 0f;
    27.     float smoothDist = 0f;
    28.     float scrollVelocity = 0;
    29.    
    30.     void Start () {
    31.         if (target.GetComponent<Rigidbody>())
    32.         {
    33.             target.GetComponent<Rigidbody>().freezeRotation = true;
    34.         }
    35.     }
    36.    
    37.     void LateUpdate () {
    38.         if (target != null)
    39.         {
    40.             x += Input.GetAxis("Mouse X") * xSpeed;
    41.             y -= Input.GetAxis("Mouse Y") * ySpeed;
    42.  
    43.             y = Mathf.Clamp(y, yMinLimit, yMaxLimit);
    44.  
    45.             xSmooth = Mathf.SmoothDamp(xSmooth, x, ref xVelocity, smoothTime);
    46.             ySmooth = Mathf.SmoothDamp(ySmooth, y, ref yVelocity, smoothTime);
    47.  
    48.             distance = Mathf.Clamp(distance + Input.GetAxis("Mouse ScrollWheel") * -distance, distanceMin, distanceMax);
    49.             smoothDist = Mathf.SmoothDamp(smoothDist, distance, ref scrollVelocity, smoothTime);
    50.  
    51.             transform.localRotation = Quaternion.Euler(ySmooth, xSmooth, 0);
    52.  
    53.             transform.position = transform.rotation * new Vector3(0.0f, 0.0f, -smoothDist) + target.position;
    54.         }
    55.     }
    56. }
    What this does is that the camera can track the object, follow it and rotate arround it, this works perfect on a plane surface, I have managed to align the player to a sphere, and parenting the camera to the player, will offset the camera's rotation, so the camera rotates arround the player and looks good, the problem is that the player have a ragdoll effect, so if I keep the camera parented to the player, the camera rotates with the player and it does not look good.

    I would ask for help to align the camera to the vector from the center of the sphere to the player, so it can rotate along that axis without weird angles.
     
  2. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,155
    You're not supposed to parent the camera to the player. You leave the camera with no parent, and drag the player object into the "Target" field of the CameraController inspector.
     
    Vitaly-Abilevich likes this.
  3. Vitaly-Abilevich

    Vitaly-Abilevich

    Joined:
    Nov 6, 2013
    Posts:
    47
    I see, thank you! :D

    I have manged to make it go, by creating an Empty Transform, then parenting the camera to that transform, that empty transform copies the location of the player and the player copies the rotation of the transform, it's weird, but it works, problem is that I can not rotate player over the Y axis, but I will eventually get it going.
     
  4. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,155
    I'm guessing you found this code somewhere on the internet? Like I said, you shouldn't need to parent the camera to anything; just put it as a root object in the scene. The code you posted is intended to make the camera follow a target; you're supposed to drag the target (the player in your case) into the Target slot on the inspector.
     
  5. Vitaly-Abilevich

    Vitaly-Abilevich

    Joined:
    Nov 6, 2013
    Posts:
    47
    Yes, I need to parent the camera to the Empty Transform, you see If I don't do that the camera will always be fixed over an axis, but if I parent it the camera to an Empty Transform, it will copy the rotation, in result the camera will properly align with the Empty Transform so it feels like standing over the surface of the planet and not like the camera orbiting it, that Empty Transform copies the location of the player and the player copies the rotation of the Empty Transform, and the empty transform has a script to get aligned to the surface of the sphere, so the player can walk on the sphere.

    This works and I do not know how to do it other way