Camera that goes around a sphere

Hey,
i need your help.
I followed the Roll-a-ball Tutorial and took it up a notch, its going very well by now but i have 1 problem.
The camera only sees in one direction, i need the camera to see in that direction, the player is moving.

Here are my Camera and player scripts:

Camera

using UnityEngine;
using System.Collections;

public class CameraController : MonoBehaviour {

    public GameObject player;

    private Vector3 offset;

    void Start()
    {
        offset = transform.position - player.transform.position;
    }

    void LateUpdate()
    {
        transform.position = player.transform.position + offset;
    }
}

Player

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class PlayerMovement : MonoBehaviour {


    public float speed;
    public float maxspeed;
    public int NeededPoints;
    public Text countText;
    public int points;
    private Rigidbody rb;
    private Vector3 camForward;
    private Transform cam;
    private Vector3 movement;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
        points = 0;
        SetCountText();
        cam = Camera.main.transform;
    }

    void FixedUpdate()
    {
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");

        camForward = Vector3.Scale(cam.forward, new Vector3(1, 0, 1)).normalized;
        movement = (moveVertical * camForward + moveHorizontal * cam.right).normalized;
        if(rb.velocity.magnitude <= maxspeed)
        {
            rb.AddForce(movement * speed);
        }
        Debug.Log(rb.velocity.magnitude);
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("green"))
        {
            other.gameObject.SetActive(false);
            points = points + 5;
        }
        else if (other.gameObject.CompareTag("red"))
        {
            other.gameObject.SetActive(false);
            points = points + 10;
        }
        else if (other.gameObject.CompareTag("blue"))
        {
            other.gameObject.SetActive(false);
            points = points + 20;
        }
        SetCountText();
    }

    void SetCountText()
    {
        countText.text = "Points: " + points.ToString() + "/" + NeededPoints.ToString();
        if (points >= NeededPoints)
        {
          
        }
    }
}

I tried many things but i don´t know anything about the camera.
Here is what i have so far: http://chakratos.comlu.com/TheEternalRoll.html
I hope you can help

LG Chakratos

To have the camera point in the direction of the player, try this after positioning the camera in LateUpdate

transform.forward = player.transform.position - transform.position;

Sadly i cant recognice any change

I tried to change the rotation of the Camera like so:

transform.rotation = new Quaternion(transform.rotation.x, player.transform.rotation.y, transform.rotation.z, transform.rotation.w);

But with that the camera just shakes around like crazy

I’m in no doubt that it would seem crazy.

I think it is the position algorithm you want to change, try this out

public class CameraController : MonoBehaviour
{
    public GameObject player;
    public float distanceBehindPlayer = 2.0f;
    public float distanceAbovePlayer = 2.0f;

    void LateUpdate()
    {
        transform.position = player.transform.position - player.transform.forward * this.distanceBehindPlayer + player.transform.up * this.distanceAbovePlayer;
        transform.forward = player.transform.position - transform.position;
    }
}

Its nearly the result i had with my try^^
I uploaded a version with this script: http://chakratos.comlu.com/TheEternalRoll.html
sadly again some shaky screen.

I would think you could simply parent the Camera to the Player in the hierarchy and it would just automatically keep the Transform offsets, no? I’m not sure if that works on rotations or not, but if not, you could have your script simply alter the rotation of the camera to match that of the player, instead of worrying about positions.

Do a quick Youtube search for “Chase Cam” or “Follow Cam” tutorials if you want to see some camera setup scripts though.

I haven’t watched it, but it sounds like what you want here:

If you would make the Camera a part of the Sphere the Camera would go in Circles around the ball and you arent able to see anything.
The video don´t show anything that would help me because he used it as a 2D camera and i already have that.

Oh, I thought you were wanting the camera to spin around behind the ball like a shoulder cam.

Ok, consider trying this, maybe…

Set an empty gameobject as a child to the Ball, place it in front of your ball or wherever works best. Then, set your camera script to do a transform.LookAt(objectName) on the empty gameobject. That’s a really simple way of getting the camera to face where you want. You could also do that LookAt on the ball, but I’m assuming you want a better view than just staring at the ball, the empty gameobject lets you direct the camera to a distance/height of your choice by simply moving the empty around.

Another option, if the ball is using physics you should be able to replace the ball’s transform.forward with rigidbody.velocity.normalized and get the correct result. Just remember not to update the camera direction when velocity is zero.

Wouldn´t that empty object spin too?

Could you give me an example of that?
I never worked with velocity.

Thanks for helping me guys :slight_smile:

Something like this:

    public class CameraController : MonoBehaviour
    {
        public GameObject player;
        public float distanceBehindPlayer = 2.0f;
        public float distanceAbovePlayer = 2.0f;
        private Rigidbody playerRigidbody;

        private void Awake()
        {
            this.playerRigidbody = this.player.GetComponent<Rigidbody>();
        }

        private void LateUpdate()
        {
            transform.position = player.transform.position - this.playerRigidbody.velocity * this.distanceBehindPlayer + Vector3.up * this.distanceAbovePlayer;
            transform.forward = player.transform.position - transform.position;
        }
    }

I haven’t tried it myself so you might need to change casing and fix other oddities I’ve missed.

I don’t know if it’s helpful, because I’m not 100% sure how it works as I’ve never used it. But similar to robin’s comment on Rigidbody, you might also check into using bounds/bounding boxes and the collider as they may be a bit more static and not affected by rotation(not certain).

Well… it… kinda works i guess ^^
Its still very glitchy but i just need to understand it and maybe it will be working^^
Result: http://chakratos.comlu.com/TheEternalRoll.html

I will look into that too, thanks for mentioning it :slight_smile:

I wouldn’t call it glitchy, but you could add smoothing into the code so the camera wouldn’t respond to fast.

public class CameraController : MonoBehaviour
    {
        public GameObject player;
        public float distanceBehindPlayer = 2.0f;
        public float distanceAbovePlayer = 2.0f;
        private Rigidbody playerRigidbody;
        private Vector3 targetPosition;
        private Quaternion targetRotation;
        public float smoothPositionSpeed = 1.0f;
        public float smoothRotationSpeed = 1.0f;
        private void Awake()
        {
            this.playerRigidbody = this.player.GetComponent<Rigidbody>();
        }
        private void LateUpdate()
        {
            this.targetPosition = player.transform.position - this.playerRigidbody.velocity * this.distanceBehindPlayer + Vector3.up * this.distanceAbovePlayer;
           this.targetRotation = player.transform.position - transform.position;
            
            this.transform.position = Vector3.Lerp(this.transform.position, this.targetPosition, Time.time * this.smoothPositionSpeed);
            this.transform.forward = Quaternion.Lerp(this.transform.position, this.targetRotation, Time.time * this.smoothRotationSpeed);
        }
    }
1 Like

Thank you very much for helping me but i get some Errors.

Sorry, my bad. Just replace all “Quaternion” in the file with “Vector3”.

Alright it works now.
But i still have some problems with it:

  1. The camera reacts way too hard on velocity, if you do something like velocity / 10 in the Camera script it gets better and worse on the same time.
  2. Even with smoothness 10 or 100 the camera “lags”. (And problem nr.1 gets worse)
  3. The camera starts on top of the sphere and you have to move left and right until you can use W and S.
  4. If you are standing still and press S the camera freaks out.

Im sorry for all that but you are my last hope.

http://chakratos.comlu.com/TheEternalRoll.html Here is your script with 10 to both smoothness.

I forgot the “normalized” property I told you to use on the velocity.

You need to append “.normalized” after “this.playerRigidbody.velocity”