Home>

Hello Unity beginner.
I checked it because I have to follow the player in the game I'm making now.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Camera: MonoBehaviour
{
    [SerializeField] private GameObject targetObj;
    private Vector3 _targetPos;
    private Vector3 _roteuler;
    // Start is called before the first frame update
    void Start ()
    {
        _targetPos = targetObj.transform.position;
        _roteuler = new Vector3 (transform.localEulerAngles.x, transform.localEulerAngles.y, 0);
    }
    // Update is called once per frame
    void Update ()
    {
        rotateCameraAngle ();
    }
    // Camera rotation control
    private void rotateCameraAngle ()
    {
        // Move yourself (camera) by the amount of movement of the target
        transform.position + = targetObj.transform.position --_ targetPos;
        _targetPos = targetObj.transform.position;
        _roteuler = new Vector3 (transform.eulerAngles.x, transform.eulerAngles.y, targetObj.transform.eulerAngles.z);
        transform.eulerAngles = _roteuler;
    }
}


Then it became the following script!
I tried my best while looking at the script reference.
https://docs.unity3d.com/en/2019.4/ScriptReference/Transform-eulerAngles.html
I was able to follow, but I want the camera to look from behind the player.
As for the behavior I want to do, I want to make it look like Mario Kart from behind.
So the Y-axis also seemed to be fixed, so I fixed it.
I wondered if I could go with loockRotation and tried it, but it didn't work
https://docs.unity3d.com/ja/current/ScriptReference/Quaternion.LookRotation.html

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Camera: MonoBehaviour
{
    [SerializeField] private GameObject targetObj;
    private Vector3 _targetPos;
    private Vector3 _roteuler;
    // Rotation limit
    [SerializeField] private float angle_LimitUp = 45f;
    [SerializeField] private float angle_LimitDown = -45f;
    // Start is called before the first frame update
    void Start ()
    {
        _targetPos = targetObj.transform.position;
        _roteuler = new Vector3 (transform.localEulerAngles.x, transform.localEulerAngles.y, 0);
    }
    // Update is called once per frame
    void Update ()
    {
        rotateCameraAngle ();
    }
    // Camera rotation control
    private void rotateCameraAngle ()
    {
        // Move yourself (camera) by the amount of movement of the target
        transform.position + = targetObj.transform.position --_ targetPos;
        _targetPos = targetObj.transform.position;
        _roteuler = new Vector3 (transform.eulerAngles.x, transform.eulerAngles.y, targetObj.transform.eulerAngles.z);
        //transform.position = targetObj.transform.up + _roteuler;
        transform.rotation = Quaternion.LookRotation (targetObj.transform.forward, targetObj.transform.up);
        transform.eulerAngles = _roteuler;
    }
}


Please answer. It will be helpful if you give us an explanation.
There is no story about having a camera as a child. I want to play with it later.

  • Answer # 1

    // Source code
    using UnityEngine;
    public class Camera: MonoBehaviour
    {
        // character
        // Can be set from Inspector
        [SerializeField]
        private Transform target;
        // Camera height
        // Can be set from Inspector
        [SerializeField, Range (1, 10)]
        private float camHeight = 5.0f;
        // Distance from the character (on the Z axis)
        // Can be set from Inspector
        [SerializeField, Range (1, 10)]
        private float camDistance = 5.0f;
        // Update is called once per frame
        void Update () {
            // Processing that goes down by the distance from the character's position and goes up by the height
            transform.position = target.position-(target.forward * camDistance) + new Vector3 (0, camHeight, 0);
            // Processing that always faces the character
            transform.LookAt (target);
        }
    }

    It's easy, but I implemented it.
    If I attach this script to the camera, I wonder if the camera work of Mao Kart is enough.

    please refer.

  • Answer # 2

    Is it not good to make the camera a child element of the player?