/
/
/ "BRO TIGER"2017-06-03 09:27:15
C++ / C#
/ "BRO TIGER", 2017-06-03 09:27:15

How to make the camera move behind the character and at the same time Rotate and Zoom in?

Good day everyone... For my game I wanted to make a camera like in an MMO like World of Warships...
I made it, but of course without a view from the front side and the gun))) Having attached the camera to the Imaginary, future ship, I realized - What -that's not it... Tried to follow the camera in different ways... And did [ offset = transform.position - target.position ] then just [ position = target.position + offset ], but then you can't rotate the camera... I mocked Distance and [ transform.forward ] in different ways, it seems to be better, but... because of [ transform.forward ], the camera literally flew away))) For the second day I have been coming up with a sophisticated plan of how to do it and not to do it major "Repair" of the script...
Thank you in advance!
Here is the script written in C#:

using UnityEngine;
using System.Collections;


public class MouseOrbitCamera : MonoBehaviour
{

    public Transform target;
    public float xSpeed = 12.0f;
    public float ySpeed = 12.0f;
    public float scrollSpeed = 10.0f;

    public float smoothing = 4f;

    public float zoomMin = 1.0f;

    public float zoomMax = 20.0f;

    float distance;

    public Vector3 position;


    float x = 0.0f;

    float y = 0.0f;


    void Start()
    {

        Cursor.lockState = CursorLockMode.Locked;

        Vector3 angles = transform.eulerAngles;

        x = angles.y;
        y = angles.x;

    }

 /* void DidLockCursor()
    {
        Debug.Log("Курсор зафиксирован");
    }
    void DidUnlockCursor()
    {
        Debug.Log("Курсор свободен");
    }
    */   
    

    void LateUpdate()
    {
        if (Input.GetKeyDown("escape"))
            Cursor.lockState = CursorLockMode.None;
            //DidUnlockCursor();

        if (Input.GetMouseButtonDown(0))
        {
            Cursor.lockState = CursorLockMode.Locked;
            //DidLockCursor();
        }

        x += Input.GetAxis("Mouse X") * xSpeed;
        y -= Input.GetAxis("Mouse Y") * ySpeed;

        transform.RotateAround(target.position, transform.up, x);    
        transform.RotateAround(target.position, transform.right, y);

        x = 0;
        y = 0;

        distance = Vector3.Distance(transform.position, target.position);

        distance = ZoomLimit(distance, zoomMin, zoomMax);

        position = -(transform.forward * distance) + target.position;

        if (Input.GetAxis("Mouse ScrollWheel") != 0)

        {

            distance = Vector3.Distance(transform.position, target.position);
            distance = ZoomLimit(distance - Input.GetAxis("Mouse ScrollWheel") * scrollSpeed, zoomMin, zoomMax);
            position = -(transform.forward * distance) + target.position;

            transform.position = Vector3.Lerp(transform.position, position, smoothing * Time.deltaTime);
        }

    }

    public static float ZoomLimit(float dist, float min, float max)

    {

        if (dist < min)

            dist = min;

        if (dist > max)

            dist = max;

        return dist;
    }

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Gaydak, 2017-06-03
@BRO_TIGER

Excuse me, but the description as in World of Warships is a so-so explanation. or look for a video or even download the game to see how the camera rotates. and you "just like there" only without a couple of modes. (thank God it seems clear from the code what you want)
so, as far as I understand, you want the camera to rotate around the target in a sphere and be able to change the radius.
then would suggest to just use spherical coordinates

relativePosition = SphericalToCartesian(longitude, latitude, orbitRadius);
transform.position = target.transform.position + relativePosition;

private Vector3 SphericalToCartesian(float longitude, float latitude, float radius)
    {
        float x = radius * Mathf.Sin(latitude) * Mathf.Cos(longitude);
        float y = radius * Mathf.Cos(latitude);
        float z = radius * Mathf.Sin(latitude) * Mathf.Sin(longitude);
        return new Vector3(x, y, z);
    }

Essentially you have longitude and latitude (like on a globe) and the radius of the sphere.
Calculate the coordinates on the sphere and add them to the target position.
Change latitude, longitude and radius based on input - here I see you know how.
well, by the way, the script is here for a couple of dozen lines, and little connected with other functionality - even rewriting it all - it’s hard to call it a major overhaul

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question