Guión de movimiento de cámara simple

Se puede colocar directamente en la lente o en el objeto del juego, adecuado para ver la escena

Versión 3: operación simplificada basada en la función Version2

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

public class MOVE : MonoBehaviour
{
    
    
    public Transform leftRotateCenter;
    public Transform rightRotateCenter;
    private Transform currentRotateCenter;

    public float speedLevel = 1;
    private float mySpeed;

    private void Update()
    {
    
    
        if (Input.GetMouseButtonUp(0) || Input.GetMouseButtonUp(1))
        {
    
    
            currentRotateCenter = null;
            mySpeed = 0;
        }
        if (Input.GetMouseButtonDown(0))
        {
    
    
            currentRotateCenter = leftRotateCenter;
            mySpeed = -100;
        }
        if (Input.GetMouseButtonDown(1))
        {
    
    
            currentRotateCenter = rightRotateCenter;
            mySpeed = 100;
        }
        if (Input.GetKeyDown("-") && speedLevel > 0.5) 
        {
    
    
            speedLevel -= 0.5f;
        }
        if (Input.GetKeyDown("=") && speedLevel < 3)
        {
    
    
            speedLevel += 0.5f;

        }
    }

    private void LateUpdate()
    {
    
    
        if (currentRotateCenter != null)
        {
    
    
            this.transform.RotateAround(currentRotateCenter.position, Vector3.up, mySpeed * speedLevel * Time.deltaTime);
        }
        else if (Input.GetAxis("Mouse ScrollWheel") != 0)
        {
    
    
            if (Input.GetKey("space"))
            {
    
    
                transform.Translate(0, Input.GetAxis("Mouse ScrollWheel") * speedLevel, 0);
            }
            else
            {
    
    
                transform.Translate(0, 0, Input.GetAxis("Mouse ScrollWheel") * speedLevel);
            }
        }
    }
}

Versión 2: Admite múltiples velocidades, separa las funciones de turno y espacio.

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

public class MOVE: MonoBehaviour
{
    
    
    public float horiSpeed = 10f;
    public float vertSpeed = 3f;
    public float speed = 2f;
    public float maxSpeed = 4f;
    public float minSpeed = 1f;

    private void Update()
    {
    
    
        if (Input.GetKeyUp("=") && speed < maxSpeed)
        {
    
    
            speed += 1;
        }
        if (Input.GetKeyUp("-") && speed > minSpeed)
        {
    
    
            speed -= 1;
        }
    }
    private void LateUpdate()
    {
    
    
        if (Input.GetKey("left shift")) 
        {
    
    
            var turnArround = Input.GetAxis("Horizontal") * Time.deltaTime * horiSpeed * speed;
            var horizontal = Input.GetAxis("Vertical") * Time.deltaTime * horiSpeed * (speed - 0.9f);
            transform.Rotate(0, turnArround, 0);
            transform.Translate(horizontal, 0, 0);
        }
        else if (Input.GetKey("space"))
        {
    
    
            var jump = Input.GetAxis("Vertical") * Time.deltaTime * horiSpeed * 2 ;
            transform.Translate(0, jump, 0);
        }
        else
        {
    
    
            var horizontal = Input.GetAxis("Vertical") * Time.deltaTime * horiSpeed * speed;
            var vertical = Input.GetAxis("Horizontal") * Time.deltaTime * horiSpeed * speed;
            transform.Translate(horizontal, 0, vertical);
        }
    }
}

Versión 1: la función de espacio se eleva y la función de cambio se desacelera

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

public class MOVE : MonoBehaviour
{
    
    
    public float fly = 10f;
    public float Vert = 20.0f;
    public float Hori = 100f;


    void Update()
    {
    
    
        if (Input.GetKeyUp("left shift"))
        {
    
    
            Vert = 20f;
            Hori = 100f;
            fly = 10f;
        }
        if (Input.GetKeyDown("left shift"))
        {
    
    
            Vert = 4f;
            Hori = 20f;
            fly = 2f;

        }
        
        if (Input.GetKey("space"))
        {
    
    
            var y = Input.GetAxis("Horizontal") * Time.deltaTime * Hori;
            var x = Input.GetAxis("Vertical") * Time.deltaTime * Vert;
            transform.Translate(0, x, 0);
            transform.Rotate(0, y, 0);
        }
        else
        {
    
    
            var y = Input.GetAxis("Horizontal") * Time.deltaTime * Hori;
            var z = Input.GetAxis("Vertical") * Time.deltaTime * Vert;
            transform.Translate(y, 0, z);
        }
}


La cámara rodea el guión del objeto.

Supongo que te gusta

Origin blog.csdn.net/MikeW138/article/details/89185569
Recomendado
Clasificación