• 一、项目创建与角色移动


    目录

    1.创建项目

    2.导入素材  

    3.搭建场景 

    4.创建玩家 


    1.创建项目

    2.导入素材  

    3D GUNS | Guns Pack | 3D 武器 | Unity Asset Storeicon-default.png?t=N7T8https://assetstore.unity.com/packages/3d/props/weapons/3d-guns-guns-pack-228975

    Prototyping Pack (Free) | 3D | Unity Asset Storeicon-default.png?t=N7T8https://assetstore.unity.com/packages/3d/prototyping-pack-free-94277 

    3.搭建场景 

    (1)选择合适的Meshes拖入场景中并调整位置 

      

    (2)创建空物体“Environment”并将场景中的物体和Directional Light置于与其内部(使用Ctrl进行多选) 

    (3)选择空物体“Environment”,调整Scale属性使其成比例放大

    (4)  选择合适的Materials拖至对应物体

     效果图如下: 

    4.创建玩家 

    (1) 创建空物体“Player”并在其内部创建同为空物体“Graphis”和Camera,在“Graphis内部”创建一个球体作为玩家模型。Hierarchy中结构应如下:

    对空物体“Player”添加Sphere Collider组件和Rigidbody组件 

    Rigidbody组件的设置如下:  

     (2)选择合适的枪械素材,将其拖入场景并置于空物体“Player”中Camera下。Hierarchy中的物体结构应如下: 

    分别先后将对应文件夹下的New Material和obstacle_texture拖至枪械素材 

    适当调整枪械相对于玩家的位置

     (4)在Asset目录下创建Prefabs目录,并将刚刚创建的Player拖入所新创建的Prefabs目录以创建预制体

    (5)在Asset目录下创建Scripts目录,用与放置所编写的各种script,并在其内部创建Player目录用于存储与Player相关的script

    (6)在Player预制体上创建并编写PlayerInput.cs,用与接收玩家输入

    1. using UnityEngine;
    2. public class PlayerInput : MonoBehaviour
    3. {
    4. [SerializeField]
    5. private float speed = 5f;
    6. [SerializeField]
    7. private PlayerControllor playerControllor;
    8. [SerializeField]
    9. private float lookSensitivity = 8f;
    10. // Start is called before the first frame update
    11. void Start()
    12. {
    13. Cursor.lockState = CursorLockMode.Locked;
    14. }
    15. // Update is called once per frame
    16. void Update()
    17. {
    18. float xMov = Input.GetAxisRaw("Horizontal");
    19. float yMov = Input.GetAxisRaw("Vertical");
    20. Vector3 velocity = (transform.right * xMov + transform.forward * yMov).normalized*speed;
    21. playerControllor.Move(velocity);
    22. float xMouse = Input.GetAxisRaw("Mouse X");
    23. float yMouse = Input.GetAxisRaw("Mouse Y");
    24. Vector3 yRotation = new Vector3(0f, xMouse, 0f)*lookSensitivity;
    25. Vector3 xRotation = new Vector3(-yMouse, 0f, 0f)*lookSensitivity;
    26. playerControllor.Rotate(yRotation,xRotation);
    27. }
    28. }

    :可在Edit>Project Setting>Input Manager中查看各按键的对应关系

    (7)在Player预制体是创建并编写PlayerController.cs,用于控制玩家行为

    1. using UnityEngine;
    2. public class PlayerControllor : MonoBehaviour
    3. {
    4. [SerializeField]
    5. private Rigidbody rb;
    6. [SerializeField]
    7. private Camera _camera;
    8. private Vector3 velocity = Vector3.zero;//速度:每秒钟移动的距离
    9. private Vector3 yRotation=Vector3.zero;//旋转角色
    10. private Vector3 xRotation = Vector3.zero;//旋转视角
    11. public void Move(Vector3 _velocity)
    12. {
    13. velocity = _velocity;
    14. }
    15. public void Rotate(Vector3 _yRotation, Vector3 _xRotation)
    16. {
    17. yRotation = _yRotation;
    18. xRotation = _xRotation;
    19. }
    20. private void PerformMovement()
    21. {
    22. if (velocity != Vector3.zero)
    23. {
    24. rb.MovePosition(rb.position+velocity*Time.fixedDeltaTime);
    25. }
    26. }
    27. private void PerformRotation()
    28. {
    29. if (yRotation != Vector3.zero)
    30. {
    31. rb.transform.Rotate(yRotation);
    32. }
    33. if (xRotation != Vector3.zero)
    34. {
    35. _camera.transform.Rotate(xRotation);
    36. }
    37. }
    38. private void FixedUpdate()
    39. {
    40. PerformMovement();
    41. PerformRotation();
    42. }
    43. }

    将(6)(7)所编写的PlayerInput.cs和PlayerController.cs移动至Assets/Scripts/Player

  • 相关阅读:
    学习笔记之——C语言 函数
    第十一章第一节:JavaString类介绍和常用方法
    手撕常见JS面试题
    序列模型之循环神经网络(二)
    SSGSSRCSR区别
    【密码学篇】虚拟专用网技术原理与应用(商密)
    【YOLO系列】YOLOv9论文超详细解读(翻译 +学习笔记)
    NLDNLD
    〔020〕Stable Diffusion 之 骨骼姿势 篇
    一款跳转警告HTML单页模板源码
  • 原文地址:https://blog.csdn.net/qq_53401568/article/details/133419817