該腳本掛載到需要被移動(dòng)、旋轉(zhuǎn)控制的物體身上,也可以之間掛在到攝像機(jī)上!
掛載到攝像機(jī)上可以實(shí)現(xiàn)第一人稱視角控制!
掛載到物體身上,配合攝像機(jī)跟蹤腳本可以實(shí)現(xiàn),第三人稱視角控制!?
第一人稱視角
將角色控制腳本掛給攝像機(jī)即可!
以下是角色控制腳本:
// 導(dǎo)入U(xiǎn)nity的命名空間,以便我們可以使用Unity的類和方法
using System.Collections;
// 導(dǎo)入U(xiǎn)nity的Generic Collections命名空間,以便我們可以使用Generic Collections類和方法
using System.Collections.Generic;
// 導(dǎo)入U(xiǎn)nityEngine命名空間,這個(gè)命名空間包含了很多Unity的類和方法
using UnityEngine;
// 定義一個(gè)名為RotateControl的公共類,這個(gè)類繼承自MonoBehaviour類
// MonoBehaviour類是Unity中的基礎(chǔ)類,用于讓游戲?qū)ο蠛推渌M件進(jìn)行交互
public class RotateControl : MonoBehaviour
{
// 這個(gè)注釋是對(duì)該腳本作用的簡(jiǎn)單描述
// 這個(gè)腳本來通過鼠標(biāo)左右滑動(dòng)和上下滑動(dòng)控制攝像機(jī)或者主角進(jìn)行選擇
/*
* 下面的注釋是對(duì)整個(gè)流程的描述,包括檢測(cè)用戶是否有滑動(dòng)鼠標(biāo)以及滑動(dòng)后如何旋轉(zhuǎn)的說明
*/
// 定義一個(gè)浮點(diǎn)型變量RotionXX,這個(gè)變量用于存儲(chǔ)旋轉(zhuǎn)角度(在X軸上)
float RotionXX;
// 定義一個(gè)浮點(diǎn)型變量RotionYY,這個(gè)變量用于存儲(chǔ)旋轉(zhuǎn)角度(在Y軸上)
float RotionYY;
// 定義一個(gè)公共浮點(diǎn)型變量roatespeed,這個(gè)變量用于控制旋轉(zhuǎn)的速度,默認(rèn)值為10
public float roatespeed = 10f;
// Update方法在Unity中是每個(gè)幀都會(huì)調(diào)用的方法,類似于C++中的每幀都會(huì)執(zhí)行一次的函數(shù)
void Update()
{
// Input.GetAxis("Mouse X")獲取鼠標(biāo)在X軸上的移動(dòng)量,這個(gè)值是-1(左),0(無移動(dòng))或1(右)
float RotionY = Input.GetAxis("Mouse X");
// Input.GetAxis("Mouse Y")獲取鼠標(biāo)在Y軸上的移動(dòng)量,這個(gè)值是-1(上),0(無移動(dòng))或1(下)
float RotionX = Input.GetAxis("Mouse Y");
// 下面的代碼是計(jì)算旋轉(zhuǎn)角度并存儲(chǔ)到RotionXX和RotionYY中
// Time.deltaTime獲取上一幀和當(dāng)前幀之間的時(shí)間差,乘以roatespeed可以得到每幀旋轉(zhuǎn)的角度
// 因?yàn)槊繋臅r(shí)間可能不同,使用Time.deltaTime可以保證在不同幀率下旋轉(zhuǎn)的速度是一致的
//四元數(shù)
RotionXX += -RotionX * Time.deltaTime * roatespeed;
RotionYY += RotionY * Time.deltaTime * roatespeed;
// 這行代碼將RotionXX的值限制在-45和45之間,超過這個(gè)范圍的數(shù)值會(huì)被調(diào)整到這個(gè)范圍之內(nèi)
RotionXX = Mathf.Clamp(RotionXX, -45f, 45f);
// 打印出當(dāng)前的旋轉(zhuǎn)角度RotionXX的值,幫助我們調(diào)試程序
Debug.Log("角度" + RotionXX);
// 這行代碼將計(jì)算出來的旋轉(zhuǎn)角度應(yīng)用到對(duì)象的transform上,實(shí)現(xiàn)旋轉(zhuǎn)效果
this.transform.rotation = Quaternion.Euler(RotionXX, RotionYY, 0);
}
}
第三人稱
將上面, 角色控制腳本掛給模型,并添加一個(gè)攝像機(jī)追蹤空物體,便于攝像機(jī)有一個(gè)追蹤目標(biāo),然后把下面腳本掛給攝像機(jī)
附贈(zèng)攝像機(jī)跟蹤腳本(如果你角色控制腳本給了攝像機(jī),那么這個(gè)腳本就不需要了)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class CameraFollow : MonoBehaviour
{
? ? public Transform target; // 跟蹤目標(biāo)
? ? public float smoothTime = 0.3f; // 平滑時(shí)間? ? private Vector3 velocity = Vector3.zero;
? ? void Update()
? ? {
? ? ? ? 計(jì)算新的位置
? ? ? ? Vector3 targetPosition = target.TransformPoint(new Vector3(0,1,-5));//本地坐標(biāo)轉(zhuǎn)世界坐標(biāo)
? ? ? ? 平滑移動(dòng)到新的位置
? ? ? ? transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, smoothTime);
? ? ? ? transform.LookAt(target);
? ? }
}
附上一個(gè)官方的攝像機(jī)視角控制腳本,這個(gè)腳本需要掛在到攝像機(jī)文章來源:http://www.zghlxwxcb.cn/news/detail-736190.html
using UnityEngine;
namespace Unity.AI.Navigation.Samples
{
/// <summary>
/// Manipulating the camera with standard inputs
/// </summary>
public class FreeCam : MonoBehaviour
{
public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivityX = 15F;
public float sensitivityY = 15F;
public float minimumX = -360F;
public float maximumX = 360F;
public float minimumY = -60F;
public float maximumY = 60F;
public float moveSpeed = 1.0f;
public bool lockHeight = false;
float rotationY = 0F;
void Update()
{
if (axes == RotationAxes.MouseXAndY)
{
float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
else if (axes == RotationAxes.MouseX)
{
transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
}
else
{
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
}
var xAxisValue = Input.GetAxis("Horizontal");
var zAxisValue = Input.GetAxis("Vertical");
if (lockHeight)
{
var dir = transform.TransformDirection(new Vector3(xAxisValue, 0.0f, zAxisValue) * moveSpeed);
dir.y = 0.0f;
transform.position += dir;
}
else
{
transform.Translate(new Vector3(xAxisValue, 0.0f, zAxisValue) * moveSpeed);
}
}
}
}
?文章來源地址http://www.zghlxwxcb.cn/news/detail-736190.html
到了這里,關(guān)于Unity角色或攝像機(jī)移動(dòng)和旋轉(zhuǎn)的控制腳本的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!