• 关于用过控制人物移动的方法


    在这里将收集更新有关,控制移动的方法。

    1.通过WSAD控制人物的前后移动的方法(一)

     1 public class PlayerMovemeng:monoBehaviour
     2 {
     3 //人物的移动速度
     4 public float speed=6f;
     5 //坐标的移动变化量
     6 Vector3 movement;
     7 Rigidbody playerRigidbody;
     8 //与Ray发生碰撞的平面的Layer层数’
     9 int FloorMask;
    10 float camRayLength=100f;
    11 void Awake()
    12 {
    13 floorMask=LayerMask.GetMask("Floor");
    14 PlayerRigidbody=GetComponent<Rigidbody>();
    15 }
    16 void FixedUpdate()
    17 {
    18 //ws前后的控制
    19 float h=Input.GetAxisRaw("Horizontal"):
    20 //ad左右的控制
    21 float v=Input.GetAxisRaw("Vertical")
    22 Move(h,v);
    23 Turning();
    24 }
    25 //实现移动
    26 void Move(float h,float v)
    27 {
    28 movement.Set(h,0,v);
    29 movement=movement.normalized*speed*Time.deltaTime;
    30 //使用刚体的MovePosition来实现人物移动
    31 playerRigidbody.MovePosition(trandorm.position+movement);
    32 }
    33 //转向鼠标
    34 void Turning()
    35 {
    36 Ray camRay=Cameta.main.ScreenPointToRay(Input.mousePosition);
    37 RaycastHit floorHit;
    38 if(Physics.Raycast(camRay,Out floorHit,camRayLength,floorMask));
    39 {
    40 //获取人物与鼠标点击处的向量
    41 Vevtor3 playerToMouse=floorHit.point-transform.position;
    //获取人物与鼠标之间的用于旋转的四元数
    42 Quaternion newRotation=Quaternion.LookRotation(playerToMouse);
    //实现人物的转向过度
    43 playerRigidbody.MoveRotation(newRotation); 44 } 45 } 46 }

    2.实现相机跟随人物的脚本(无旋转方向)

     1 public Transform target;
     2 //镜头滑动速度
     3 public float smoothing=5f;
     4 //相机与人物的距离
     5 Vector3 offset;
     6 void Start()
     7 {
     8 offset=transform.positiion-target.position;
     9 }
    10 void FixedUpdate()
    11 {
    12    Vevtor3 targetCamPos=target.position+offset;
    13 transform.position=Vector3.Lerp(transform.position,targetCamPos,smoothing*Time.datle);
    14 }
  • 相关阅读:
    Linux命令--2
    Linux命令--1
    登陆网页模板
    前端笔记 (2.CSS)
    前端笔记 (1.HTML)
    linux 在执行命令过程中,反单引号(`)这个符号代表的意义为何?
    linux 变量
    Linux文件系统中的inode节点详细介绍
    【Ctrl】 + 【Alt】 + 【F1~F6】 和 【Ctrl】 + 【Alt】 + 【T】打开的终端有什么不同?
    快速建站教程
  • 原文地址:https://www.cnblogs.com/HeYIcengshuo/p/6435160.html
Copyright © 2020-2023  润新知