Problem with a third person view camera

Hello,

I followed the Roll-a-ball tutorial : https://unity3d.com/fr/learn/tutorials/projects/roll-ball-tutorial

And I want to add some more options to that. The camera is fine, but I would like to know where I go and with my mouse, I would like to be able to rotate the camera around the player, so I followed a tutorial and I wrote this code :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FollowngSphere : MonoBehaviour {


    private const float Y_ANGLE_MIN = 0.0f;
    private const float Y_ANGLE_MAX = 50.0f;
    public Transform lookAt;
    public Transform camTransform;
    private Camera cam;
    private float distance = 10.0f;
    private float currentX = 0.0f;
    private float currentY = 0.0f;
    private float sensivityX = 4.0f;
    private float sensivityY = 1.0f;



    void Start()
    {
        camTransform = transform;
        cam = Camera.main;

    }

    private void Update()
    {
        currentX += Input.GetAxis("Mouse X");
        currentY += Input.GetAxis("Mouse Y");

        currentY = Mathf.Clamp(currentY, Y_ANGLE_MIN, Y_ANGLE_MAX);
    }

    void LateUpdate()
    {
        Vector3 dir = new Vector3(0, 0, -distance);
        Quaternion rotation = Quaternion.Euler(currentY, currentX, 0);
        camTransform.position = lookAt.position + rotation * dir;
        transform.LookAt(lookAt.position);

    }
}

So I can rotate my mouse around the player. The problem is, when i rotate to 180 degree, the inputs will be inversed, when i go left, it goes right, when i go up, it goes down. The more I rotate, the more the inputs will be inversed. Is there a solution to solve this problem ?

Thanks !

I would suggest that you try the free look camera prefab from the standard assets. You can use it as-is or learn from it (or just use the prefab design… ie: just the game object setup… whatever ya like! :))

Thanks for your answer. But the camera doesn’t follow my sphere. And anyway, it doesn’t solve the problem. I made a video :

In this video, I’m rotating my mouse to 180 degree, press up (but it goes down), then press down (but it goes up).

I’m sorry, did you mean the freecam prefab doesn’t follow your sphere or did you mean you don’t want it to follow the sphere?

I would like it to follow the sphere, but it doesn’t. In the video, when i press up (and it goes down), the sphere is out of the screen, but I don’t want that.

So I tried many things, I followed a tutorial where the camera follow the position and the rotation of the player, but what’s happening is this :

Code :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FollowngSphere : MonoBehaviour {

    [SerializeField]
    private Transform target;

    [SerializeField]
    private Vector3 offsetPosition;

    [SerializeField]
    private Space offsetPositionSpace = Space.Self;

    [SerializeField]
    private bool lookAt = true;

    private void LateUpdate()
    {
        Refresh();
    }
    public void Refresh()
    {
        if (target == null)
        {
            Debug.LogWarning("Missing target ref !", this);

            return;
        }

        // compute position
        if (offsetPositionSpace == Space.Self)
        {
            transform.position = target.TransformPoint(offsetPosition);
        }
        else
        {
            transform.position = target.position + offsetPosition;
        }

        // compute rotation
        if (lookAt)
        {
            transform.LookAt(target);
        }
        else
        {
            transform.rotation = target.rotation;
        }
    }
}

Maybe you also want the code of my sphere moving :

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

    public float speed;

    private Rigidbody rb;

    void Start ()
    {
        rb = GetComponent<Rigidbody>();
    }

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

        Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);

        rb.AddForce (movement * speed);
    }
}

I think the problem is my character is a sphere and it rotates a lot, so the camera does… Is there a solution to solve that ? I would like the same camera as here :

But when I turn, or go south, i would like the camera turn with the player, so I can always see in front of me. And also,I still have the problem with inverted inputs when the camera is at 180 degree of my player.

This is confusing me a little. At first I thought you wanted free control of the camera to look around the player sphere. Now, I think you’re asking how to have it always “behind” ?

Yes it’s what i meant, always behind. Is there a way for the sphere to have the camera always behind ? But without rotating like crazy like you can see in my video, and without inverted inputs ?

Well, of course you can position things relatively and what not…
bear in mind, though, that if you are always behind, then your controls would be non-intuitive sometimes, unless you changed those as well…
then, you no longer want to rotate the camera around the player? or you want it to be behind but can also rotate around? That’s the part that was confusing me…

Sadly, I do not really know what the inverted issue was at the beginning of this thread, but when I thought you wanted to rotate nicely around the character, and I suggested the freelook cam prefab, it would do that.

As for the spinning, is the camera a child of the ball? Or , in your code example, do you have “lookAt” as false?

If i can have both, mouse rotating, and the camera always behind when i press the arrows, it would be perfect. But the most important is the camera behind. I unchecked “Use gravity” and my sphere stopped rotating (except when it touches a wall), so did the camera. Is there a way my sphere doesn’t rotate vertically ? So the camera will not move vertically. I’ll still have a the inputs inverted problem, but at least, it’ll solve one problem

I tried to follow this thread for inverted inputs, but that didn’t work : Rotating object with mouse gets reversed on reversed side. - Questions & Answers - Unity Discussions

Maybe this code can help someone to help me ?

I’ve just tried something like that :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class ControllingSphere : MonoBehaviour {
    public Camera invertedAxes;
    private void FixedUpdate()
    {
if (invertedAxes.transform.rotation.y >= 90)
        {
            float moveHorizontal = Input.GetAxis("Horizontal");
            float moveVertical = Input.GetAxis("Vertical");

            Vector3 movement = new Vector3(-moveHorizontal, 0.0f, -moveVertical);

            rb.AddForce(movement * speed);  // Controlling the character
        }
        else
        {
            float moveHorizontal = Input.GetAxis("Horizontal");
            float moveVertical = Input.GetAxis("Vertical");

            Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);

            rb.AddForce(movement * speed);  // Controlling the character
        }
}
}

I think that can work, but I don’t know what I’m doing wrong. In fact, it’s like : When the camera is at 90 degree of my sphere, it changes the inputs. Maybe that can work ? But I don’t know why the code I did didn’t work…

Ok, I did that and it seems to work (about the inputs), i can still improve it, but at least, i know what to do now :

    private void FixedUpdate()
    {

        if (invertedAxes.transform.localEulerAngles.y >= 90 && invertedAxes.transform.localEulerAngles.y <= 270 )
        {
            float moveHorizontal = Input.GetAxis("Horizontal");
            float moveVertical = Input.GetAxis("Vertical");

            Vector3 movement = new Vector3(-moveHorizontal, 0.0f, -moveVertical);

            rb.AddForce(movement * speed);  // Controlling the character
        }
        else
        {
            float moveHorizontal = Input.GetAxis("Horizontal");
            float moveVertical = Input.GetAxis("Vertical");

            Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);

            rb.AddForce(movement * speed);  // Controlling the character
        }

    }