unity3d 第三人稱視角的人物移動以及相機控制

何謂第三人稱?就像這樣:html

總結一下前段時間作的rpg遊戲demo
用wasd控制人物移動,同時保持在相機的中心。用鼠標右鍵與滾輪控制相機的角度和距離。
先說一下人物的移動:
首先給做爲主角的單位加上 Charactor Controller組件,並調整膠囊型的碰撞體恰好包裹住主角(有其是腳底,除非你想看到你的主角能遁地,或飛行)
unity3d <wbr>第三人稱視角的人物移動以及相機控制
而後給你的人物加上控制的腳本~
using UnityEngine;
using System.Collections;

public class move_controll : MonoBehaviour {
Transform m_transform,m_camera;//人物本身以及相機的對象
CharacterController controller;//Charactor Controller組件
public float MoveSpeed = 20.0f;//移動的速度
// Use this for initialization
void Start () {
m_transform = this.transform;//儘可能不要再update裏獲取this.transform,而是這樣保存起來,這樣能節約性能
m_camera = GameObject.FindGameObjectWithTag ("MainCamera").transform;//
controller=GetComponent();
}
// Update is called once per frame
void Update () {
if ((Input.GetKey (KeyCode.W)) || (Input.GetKey (KeyCode.S)) || (Input.GetKey (KeyCode.A)) || (Input.GetKey (KeyCode.D))) {
transform.GetComponent().SetFloat("speed", "run");//將人物的動畫改成移動狀態,這裏有個問題,就是動畫組件的獲取也要在update裏獲取,請讀者自行修改吧
if (Input.GetKey (KeyCode.W)) {
//根據主相機的朝向決定人物的移動方向,下同
controller.transform.eulerAngles = new Vector3 (0, m_camera.transform.eulerAngles.y, 0);
}

if (Input.GetKey (KeyCode.S)) {
controller.transform.eulerAngles = new Vector3 (0, m_camera.transform.eulerAngles.y+180f, 0);
}

if (Input.GetKey (KeyCode.A)) {
controller.transform.eulerAngles = new Vector3 (0, m_camera.transform.eulerAngles.y+270f, 0);
}

if (Input.GetKey (KeyCode.D)) {
controller.transform.eulerAngles = new Vector3 (0, m_camera.transform.eulerAngles.y+90f, 0);
}

controller.Move(m_transform.forward * Time.deltaTime * MoveSpeed);
}
else
//靜止狀態
transform.GetComponent().SetFloat("speed", "stand");
if (Input.GetKey (KeyCode.Q)) {
transform.Translate (Vector3.up * Time.deltaTime * MoveSpeed);
}
if (!controller.isGrounded) {
//模擬簡單重力,每秒降低10米,固然你也能夠寫成拋物線
controller.Move(new Vector3(0,-10f*Time.deltaTime,0));
}
}

而後是相機控制的腳本,從別的地方抄的,應該也不難理解,綁定在場景主相機上便可:app

using UnityEngine;

public class CameraOrbit : MonoBehaviour 
{
    public Transform pivot; // the object being followed
public Vector3 pivotOffset = Vector3.zero; // offset from target's pivot
public Transform target; // like a selected object (used with checking if objects between cam and target)

public float distance = 10.0f; // distance from target (used with zoom)
public float minDistance = 2f;
public float maxDistance = 15f;
public float zoomSpeed = 1f;

    public float xSpeed = 250.0f;
    public float ySpeed = 120.0f;

public bool allowYTilt = true;
    public float yMinLimit = 30f;
    public float yMaxLimit = 80f;

    private float x = 0.0f;
    private float y = 0.0f;

private float targetX = 0f;
private float targetY = 0f;
private float targetDistance = 0f;
private float xVelocity = 1f;
private float yVelocity = 1f;
private float zoomVelocity = 1f;

    void Start()
    {
        var angles = transform.eulerAngles;
        targetX = x = angles.x;
targetY = y = ClampAngle(angles.y, yMinLimit, yMaxLimit);
targetDistance = distance;
    }

    void LateUpdate()
    {
        if (pivot)
        {
float scroll = Input.GetAxis("Mouse ScrollWheel");

if (scroll > 0.0f) targetDistance -= zoomSpeed;
else if (scroll < 0.0f) targetDistance += zoomSpeed;
targetDistance = Mathf.Clamp(targetDistance, minDistance, maxDistance);

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// right mouse button must be held down to tilt/rotate cam
// or player can use the left mouse button while holding Ctr
if (Input.GetMouseButton(1) || (Input.GetMouseButton(0) && (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) ))
            {
                targetX += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
if (allowYTilt)
{
targetY -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
targetY = ClampAngle(targetY, yMinLimit, yMaxLimit);
}
            }
x = Mathf.SmoothDampAngle(x, targetX, ref xVelocity, 0.3f);
if (allowYTilt) y = Mathf.SmoothDampAngle(y, targetY, ref yVelocity, 0.3f);
else y = targetY;
Quaternion rotation = Quaternion.Euler(y, x, 0);
distance = Mathf.SmoothDamp(distance, targetDistance, ref zoomVelocity, 0.5f);

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// apply
Vector3 position = rotation * new Vector3(0.0f, 0.0f, -distance) + pivot.position + pivotOffset;
transform.rotation = rotation;
transform.position = position;

        }
    }

private float ClampAngle(float angle, float min, float max)
{
if (angle < -360) angle += 360;
if (angle > 360) angle -= 360;
return Mathf.Clamp(angle, min, max);
}
}
相關文章
相關標籤/搜索