unity学习之第一人称视角

首先创建一个空的游戏体,在给游戏体挂上 Character Controller 组件,为主角添加一个角色控制器组件,这个组件可以实现控制主角移动的同时与场景的碰撞产生交互,不如不会跑到墙里去。

在添加 Rigidbody 组件,同样要取消重力模拟,并选中 Is Kinematic 复选框使其不受物理演算影响,这样才能移动

还有就是摄像机问题:
需要让摄像机伴随主角移动
添加脚本代码如下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// 
/// 主角组件
/// 
public class Player : MonoBehaviour {

    public Transform m_transform;
  
    CharacterController m_ch;        // 角色控制器组件    
    float m_movSpeed = 8.0f;         // 角色移动速度   
    float m_gravity = 10.0f;          // 重力   
    public int m_life = 5;           // 生命  
    Transform m_camTransform;        // 摄像机 Transform  
    Vector3 m_camRot;                // 摄像机旋转角度   
    float m_camHeight = 4.5f;        // 摄像机高度(即表示主角高度)

  // Use this for initialization
    void Start () {
        m_transform = this.transform;        
        m_ch = this.GetComponent<CharacterController>();    // 获取角色控制器组件
        m_camTransform = Camera.main.transform;             // 获取摄像机
        // 设置摄像机初始位置(使用TransformPoint 获取 Player 在 Y 轴偏移一定高度的位置)
        m_camTransform.position = m_transform.TransformPoint(0, m_camHeight, 0);
        // 设置摄像机的旋转方向与主角一致
        m_camTransform.rotation = m_transform.rotation;     
        m_camRot = m_camTransform.eulerAngles;
        Screen.lockCursor = true;                           // 锁定鼠标
    }
	
	// Update is called once per frame
	void Update () {
        // 如果生命值为0,什么也不做
        if (m_life <=0)
            return;
        Control();        
	}

    void Control()
    {
        // 获取鼠标移动距离
        float rh = Input.GetAxis("Mouse X");
        float rv = Input.GetAxis("Mouse Y");

        // 旋转摄像机
        m_camRot.x -= rv;
        m_camRot.y += rh;
        m_camTransform.eulerAngles = m_camRot;

        // 使主角的面向方向与摄像机一致
        Vector3 camrot = m_camTransform.eulerAngles;
        camrot.x = 0; camrot.z = 0;
        m_transform.eulerAngles = camrot;

        // 定义三个值控制移动
        float xm = 0, ym = 0, zm = 0;
       
        // 控制主角移动代码
        // 重力运动
        ym -= m_gravity * Time.deltaTime;

        // 上下左右运动
        if (Input.GetKey (KeyCode .W ))        // 按 W 键
        {
            zm += m_movSpeed * Time.deltaTime;
        }
        else if (Input.GetKey (KeyCode.S ))    // 按 S 键
        {
            zm -= m_movSpeed * Time.deltaTime;
        }
        if (Input.GetKey (KeyCode.A ))           // 按 A 键
        {
            xm -= m_movSpeed * Time.deltaTime;
        }
        else if (Input.GetKey(KeyCode.D ))      // 按 D 键
        {
            xm += m_movSpeed * Time.deltaTime;
        }

        // 使用角色控制器提供的Move函数进行移动 它会自动检测碰撞
        m_ch.Move(m_transform.TransformDirection(new Vector3(xm, ym, zm)));

        // 更新摄像机位置(始终与 Player 一致)
        m_camTransform.position = m_transform.TransformPoint(0, m_camHeight, 0);

    }

    // 在编辑器中为主角显示一个图标
    private void OnDrawGizmos()
    {
        Gizmos.DrawIcon(this.transform.position, "Spawn.tif");
    }
}

通过控制鼠标旋转摄像机方向,使主角跟随摄像机的 Y 轴旋转方向,在移动主角时,又使摄像机跟随主角运动。运行游戏就可以自由在场景中动了。

你可能感兴趣的:(unity学习之第一人称视角)