Unity中给Player设置第一人称视角的3个方法

本文介绍了在Unity中设置第一人称视角的三种方法:1) 摄像机作为胶囊子物体并添加代码;2) 使用角色控制器;3) 导入Unity的预置First Person Controller组件。每个方法都有详细步骤,并提醒了注意事项,如第一人称和第三人称视角不能同时存在。
摘要由CSDN通过智能技术生成

1.

摄像机成为胶囊的子物体,摄像机上放代码:

using UnityEngine;
using System.Collections;

public class Sj : MonoBehaviour {
    public GameObject player;           //前台拖入胶囊
    Vector3 rot = new Vector3(0, 0, 0);   //先定义一个Vectory3类型的变量rot(0,0,0)
   public float speed;           //这个是鼠标灵敏度
    void Start() {

    }

    void Update() {
        float MouseX = Input.GetAxis("Mouse X")*speed;       
        float MouseY = Input.GetAxis("Mouse Y")*speed;
        rot.x = rot.x - MouseY;
        rot.y = rot.y + MouseX;  
        rot.z = 0;   //锁定摄像头移动的角度z轴,防止左右倾斜
        transform.eulerAngles = rot;   //所有方向设定好后,摄像头的角度=rot
        player.transform.eulerAngles = new Vector3(0, rot.y, 0);  //角色角度只能通过MouseX改变大小,也就是锁定rot.y
    }
}
场景加上碰撞器

Player胶囊身上加上如下代码:

using UnityEngine;
using System.Collections;

public class PlayerMove : MonoBehaviour {
    public float speed;
    // Use this for initialization
    void Start() {

    }

    // Update is called once per frame
    void Update() {
        float MoveX = Input.GetAxis("Horizontal");
        float MoveY = Input.GetAxis("Vertical");
        transform.Translate(new Vector3(MoveX, 0, MoveY) *
            Time.deltaTime * speed);
        if (Input.GetButtonDown("Jump")) {
            Rigidbody rig = GetComponent<Rigidbody>();
            rig.AddForce(new Vector3(0, 0.5f, 0), ForceMode.Impulse);
        }
    }
}
这是用刚体做的

就可以移动了

2.给胶

  • 3
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值