简单的镜头移动脚本

可以直接放镜头或gameobject上,适合观看场景

Version 3:基于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);
            }
        }
    }
}

Version 2:支持多种速度、分开了shift 和 space 的功能。

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);
        }
    }
}

Version 1:space 功能是升降,shift 功能是减速

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);
        }
}


镜头围绕物体脚本

你可能感兴趣的:(U3D)