Jump to content
Sign in to follow this  
XXD

third person character camera

Recommended Posts

coloque esse script em uma câmera e essa câmera em um objeto, a câmera estará olhando para o objeto, se um colisor(con tags speficida em tagsCollider) ficar entre a câmera e o objeto, ele se aproximará do objeto até que não haja mais colisões entre elas,espesifique as tags na lista tagsCollider, mantenha o botão direito do mouse e mova o mouse para orbitar o objeto e use a rolagem para aumentar ou diminuir o zoom, defina os campos no inspetor para especificar as velocidades, mínimo e máximo de rolagem e órbita.

 

place this script on a camera and this camera on an object, the camera will be looking at the object, if a collider (with tags defined in tagsCollider) gets between the camera and the object, it will approach the object until there are no more collisions between them, specify the tags in the tagsCollider list, hold the right mouse button and move the mouse to orbit the object and use scrolling to zoom in or out, set the fields in the inspector to specify the minimum and maximum scroll speeds. and orbit.

Screenshot_2.png.e43f3aaace4336288db2ac2c605b59ac.pngScreenshot_2.png.e43f3aaace4336288db2ac2c605b59ac.pngcamCollider.gif.70f629ef7e3faef1b32516234627bedd.gif

 

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

public class camera : MonoBehaviour
{
    [Tooltip("Rotate te player speed")]
    [SerializeField] float speed = 5f;
    [SerializeField] float scrollSpeed = 2f;
    [Tooltip("min distance of target")]
    [SerializeField] float minDistance = 4f;
    [Tooltip("Max distance of target")]
    [SerializeField] float maxDistance = 20f;
    float distance = 10f;
    [Tooltip("Min camera's viewing angle")]
    [SerializeField] float minAlgle = 10f;
    [Tooltip("Max camera's viewing angle")]
    [SerializeField] float maxAlgle = 90f;
    Transform target;
    [Tooltip("if the target is behind (out of sight) of a collider, the camera approaches the target at this speed")]
    [SerializeField] float smooth =3;
    [Tooltip("add here any tags that can't be between object and camera")]
    [SerializeField] List<string> tagsCollider;
    private Vector3 input;
    bool overGui;
    private void Start()
    {
        target = transform.parent;
        input = new Vector3(0f, 50f, 0f);
    }
    void LateUpdate()
    {
        float Camspeed = smooth;
        {
            if (Input.GetMouseButtonDown(1) && EventSystem.current.IsPointerOverGameObject())
                overGui = true;
            if (Input.GetMouseButtonUp(1))
                overGui = false;

            if (Input.GetMouseButton(1)&&!overGui)
            {
                Camspeed = 8f;
                input += new Vector3(Input.GetAxis("Mouse X") * speed, (Input.GetAxis("Mouse Y") * -1) * speed, Input.GetAxis("Mouse ScrollWheel") * speed);
            }
            if (Input.GetAxis("Mouse ScrollWheel") > 0 && !EventSystem.current.IsPointerOverGameObject())
            {
                distance -= scrollSpeed;
            }
            else if (Input.GetAxis("Mouse ScrollWheel") < 0 && !EventSystem.current.IsPointerOverGameObject())
            {
                distance += speed;
            }

            distance = Mathf.Clamp(distance, minDistance, maxDistance);
            input.y = Mathf.Clamp(input.y, minAlgle, maxAlgle);
            Quaternion rotation = Quaternion.Euler(input.y, input.x, 0);

            Vector3 position = target.position - (rotation * Vector3.forward * distance);
            position.z += 0.2f;
            position.y += 2f;

            RaycastHit hit;
            if (Physics.Raycast(target.position + Vector3.up, (transform.position - Vector3.up*2 - target.position).normalized * distance, out hit))
            {
                if (tagsCollider.Contains(hit.collider.tag))
                {
                    distance = Vector3.Distance(target.position, hit.point) - 3f;
                    Camspeed = 5;
                    transform.position = Vector3.Lerp(transform.position, position, Time.deltaTime * Camspeed);
                    transform.rotation = rotation;
                    return;
                }
            }

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

        }

    }
}

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...