Search Unity

  1. Unity 2018.1 has arrived! Read about it here
    Dismiss Notice
  2. Scriptable Render Pipeline improvements, Texture Mipmap Streaming, and more! Check out what we have in store for you in the 2018.2 Beta.
    Dismiss Notice
  3. If you couldn't join the live stream, take a peek at what you missed.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  5. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  6. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Rotate camera arround vector from spherical surface to player?

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

  1. Vitaly-Abilevich


    Nov 6, 2013
    Good day community!

    I got this code attached to a camera:

    Code (CSharp):
    1. using UnityEngine;
    3. public class CameraController: MonoBehaviour {
    5.     public Transform target;
    6.     public float distance = 5f;
    7.     public float xSpeed = 10f;
    8.     public float ySpeed = 10f;
    9.     public float scrollSpeed = 20f;
    11.     float yMinLimit = -80f;
    12.     float yMaxLimit = 80f;
    14.     float distanceMin = 5f;
    15.     float distanceMax = 50f;
    17.     float x = 0f;
    18.     float y = 0f;
    19.     float s = 0f;
    21.     float smoothTime = 0.2f;
    23.     float xSmooth = 0f;
    24.     float ySmooth = 0f;
    25.     float xVelocity = 0f;
    26.     float yVelocity = 0f;
    27.     float smoothDist = 0f;
    28.     float scrollVelocity = 0;
    30.     void Start () {
    31.         if (target.GetComponent<Rigidbody>())
    32.         {
    33.             target.GetComponent<Rigidbody>().freezeRotation = true;
    34.         }
    35.     }
    37.     void LateUpdate () {
    38.         if (target != null)
    39.         {
    40.             x += Input.GetAxis("Mouse X") * xSpeed;
    41.             y -= Input.GetAxis("Mouse Y") * ySpeed;
    43.             y = Mathf.Clamp(y, yMinLimit, yMaxLimit);
    45.             xSmooth = Mathf.SmoothDamp(xSmooth, x, ref xVelocity, smoothTime);
    46.             ySmooth = Mathf.SmoothDamp(ySmooth, y, ref yVelocity, smoothTime);
    48.             distance = Mathf.Clamp(distance + Input.GetAxis("Mouse ScrollWheel") * -distance, distanceMin, distanceMax);
    49.             smoothDist = Mathf.SmoothDamp(smoothDist, distance, ref scrollVelocity, smoothTime);
    51.             transform.localRotation = Quaternion.Euler(ySmooth, xSmooth, 0);
    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


    May 28, 2011
    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


    Nov 6, 2013
    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


    May 28, 2011
    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


    Nov 6, 2013
    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