Unity3D第一人称视角及移动,Y轴保持不变

11 篇文章 1 订阅

创建一个Capsule,在Capsule下面创建一个Camera:

 在Capsule下增加Character Controller,新增FPLook.cs(鼠标控制的第一人称视角)和FPMove.cs(键盘WSAD控制的前后左右移动):

 FPLook.cs:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FPLook : MonoBehaviour
{
    //视野转动速度
    float speedX = 10f;
    float speedY = 10f;  //上下观察范围
    float minY = -60;
    float maxY = 60;
  //观察变化量
    float rotationX;
    float rotationY;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update () 
    {
        rotationX +=  Input.GetAxis ("Mouse X")*speedX;
        rotationY +=  Input.GetAxis ("Mouse Y")*speedY;
        if (rotationX < 0) {
            rotationX += 360;
        }
        if (rotationX >360) {
            rotationX -= 360;
        }
        rotationY = Mathf.Clamp (rotationY, minY, maxY);  
        transform.localEulerAngles = new Vector3 (-rotationY, rotationX, 0);
    }
}

 FPMove.cs:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FPMove : MonoBehaviour
{
    float speed = 5f;  //移动速度
    private CharacterController characterController;

    // Start is called before the first frame update
    void Start()
    {
        characterController = this.GetComponent<CharacterController>();
    }

    // Update is called once per frame
    void Update()
    {
        //键盘控制前后
        float x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;//左右移动
        float z = Input.GetAxis("Vertical") * Time.deltaTime * speed;//前后移动

        Vector3 movement = new Vector3 (x,0,z);
        movement = transform.TransformDirection(movement);//旋转之后的转向

        Vector3 newMovement = new Vector3 (movement.x,0,movement.z);
        characterController.Move(newMovement);//移动
    }
}

起飞降落模式:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FPMove : MonoBehaviour
{
    float speed = 5f;  //移动速度
    private CharacterController characterController;

    // Start is called before the first frame update
    void Start()
    {
        characterController = this.GetComponent<CharacterController>();
    }

    // Update is called once per frame
    void Update()
    {
        //键盘控制前后
        float x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;//左右移动
        float z = Input.GetAxis("Vertical") * Time.deltaTime * speed;//前后移动
        float y = 0;

        Vector3 movement = new Vector3 (x,y,z);
        movement = transform.TransformDirection(movement);//旋转之后的转向

        if(transform.localPosition.y >= 0)
        {
            y = movement.y;
        }
        else
        {
            y = 0.002f;//根据实际情况调整
        }

        Vector3 newMovement = new Vector3 (movement.x,y,movement.z);
        characterController.Move(newMovement);//移动
    }
}

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值