首页 > 其他 > 详细

Ruby's Adventure 01

时间:2020-09-14 20:00:10      阅读:57      评论:0      收藏:0      [点我收藏+]

·博主是根据b站up主诺十一2020的教程进行的学习~

https://www.bilibili.com/video/BV1V4411W787?p=1

1.对角色的移动做了如下的编码

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 
 5 public class PlayerContal : MonoBehaviour
 6 {
 7     // Start is called before the first frame update
 8     //控制人物移动、血量
 9     public float speed = 5f;
10     void Start()
11     {
12         
13     }
14 
15     // Update is called once per frame
16     void Update()
17     {
18         float moveX = Input.GetAxisRaw("Horizontal");
19         //控制水平移动方向 A:-1 D:1 0
20         float moveY = Input.GetAxisRaw("Vertical");
21         //控制垂直方向 W:1 S:-1 0  //注意规范书写
22 
23         Vector2 position = transform.position;  //玩家自身的位置
24         position.x += moveX * Time.deltaTime;
25         position.y += moveY * Time.deltaTime;
26         //将移动返回给玩家
27         transform.position = position;
28 
29         //transform.Translate(transform.right * speed * Time.deltaTime);
30         //首次测试
31     }
32 }

将编写的C#脚本拖动到Animation上并按运行就可以对人物进行操作。Input.GetAxisRaw(string);是指在你按下设置好的按键就会返回1和-1,不按则返回0,电脑根据按键反馈来安排人物的动作。第一个float变量moveX,“Horizontal”,水平方向上的运动,moveY是“Vertical”,垂直方向上的运动。Time.deltaTime是指增量时间。

 

Ruby's Adventure 01

原文:https://www.cnblogs.com/OnlyACry/p/13667308.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!