1
\$\begingroup\$

I have a regular Camera and this script is attached to the camera. When I rotate the camera around and the camera hits an object sometimes the camera gets pushed and if the camera distance from the target is 5 then when pushed the distance is almost 0.

The variable distanceFromTarget is set to 5 by default for example the distance from the player is 5. The problem is when the camera should or hit an object the camera in most of the cases move through the objects even if the objects have a collider and in some cases sometimes the camera distance from the player is change from 5 to almost 0.

Not sure if I need to add a collider and how to the camera or to do something else in the script ?

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

public class CameraController : MonoBehaviour
{
    public Transform targetToRotateAround;

    public float distanceFromTarget = 5.0f;

    public float xSpeed = 120.0f;
    public float ySpeed = 120.0f;

    public float yMinLimit = -20f;
    public float yMaxLimit = 80f;

    public float distanceMin = .5f;
    public float distanceMax = 15f;

    public float cameraHeight;

    public bool makeTransition = false;

    private Rigidbody rigidbody;
    private float x = 0.0f;
    private float y = 0.0f;

    // Use this for initialization
    void Start()
    {
        Vector3 angles = transform.eulerAngles;
        x = angles.y;
        y = angles.x;

        rigidbody = GetComponent<Rigidbody>();

        // Make the rigid body not change rotation
        if (rigidbody != null)
        {
            rigidbody.freezeRotation = true;
        }
    }

    private void Update()
    {
        
    }

    void LateUpdate()
    {
        if (targetToRotateAround)
        {
            x += Input.GetAxis("Mouse X") * xSpeed * distanceFromTarget * 0.02f;
            y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;

            y = ClampAngle(y, yMinLimit, yMaxLimit);

            Quaternion rotation = Quaternion.Euler(y, x, 0);

            distanceFromTarget = Mathf.Clamp(distanceFromTarget - Input.GetAxis("Mouse ScrollWheel") * 5, distanceMin, distanceMax);

            RaycastHit hit;
            if (Physics.Linecast(targetToRotateAround.position, transform.position, out hit))
            {
                distanceFromTarget -= hit.distance;
            }
            Vector3 negDistance = new Vector3(0.0f, cameraHeight, -distanceFromTarget);
            Vector3 position = rotation * negDistance + targetToRotateAround.position;

            transform.rotation = rotation;
            transform.position = position;
        }
    }

    public static float ClampAngle(float angle, float min, float max)
    {
        if (angle < -360F)
            angle += 360F;
        if (angle > 360F)
            angle -= 360F;
        return Mathf.Clamp(angle, min, max);
    }
}
\$\endgroup\$
0

2 Answers 2

0
\$\begingroup\$

Why dont you use unity cinemachine it is much easeyer to create 3rd person camera and it has a component that handles passing trough objects. Here are the docs: https://docs.unity.cn/Packages/[email protected]/manual/index.html

\$\endgroup\$
1
\$\begingroup\$

If you want to use the camera as a physics object that treats other rigidbodies as obstacles. You have to first make sure a rigidbody and collider is attatched to the camera's GameObject. You then have to make sure to use rigidbody.MovePosition(pos); and rigidbody.MoveRotation(rot); instead of transform.position = pos; and transform.rotation = rot.

For your problem with the camera teleporting almost instantly to the player when it's trying to avoid an obstacle... For third person camera's the name of the game is to be predictive so you can smoothly interpolate camera movements. Instead of linecasting to see if the camera is be obsccured, you could try line casting to predict if the camera will be obscured and pre-emptively start zooming the camera towards or away from the player accordingly. Third person camera's are hard though --> they're not solved mathematical problems.

Here's a GDC talk by the person who worked on Journey's third person camera which might help. Good luck!

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .