首页 > 技术文章 > Ruby's Adventure 01 人物移动

OnlyACry 2020-09-14 18:27 原文

 

素材来自于Unity社区官方教程~

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是指增量时间。

 注:类名和脚本名必须一致!不然会发生错误!!!

因为我的电脑很渣,人物移动很慢,重新设置了人物代码。

 

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

public class PlayerContal : MonoBehaviour
{
    // Start is called before the first frame update
    //控制人物移动、血量
    public float speed = 5f;
    Rigidbody2D rbody;  //刚体组件

    private Vector2 currentInput;
    void Start()
    {
        rbody = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        float moveX = Input.GetAxis("Horizontal");
        //控制水平移动方向 A:-1 D:1 0
        float moveY = Input.GetAxis("Vertical");
        //控制垂直方向 W:1 S:-1 0  //注意规范书写
        Vector2 movement = new Vector2(moveX, moveY);
        currentInput = movement;
        //transform.Translate(transform.right * speed * Time.deltaTime);
        //首次测试
    }

    private void FixedUpdate()
    {
        Vector2 position = rbody.position;  //玩家自身的位置
        position += currentInput * Time.deltaTime;
        //将移动返回给玩家
        //transform.position = position;
        rbody.MovePosition(position);
    }
} 

 FixedUpdate可以规定一个帧更新的时间,这样的话人物的移动速率就不会受机器配置的影响~

复习了一遍(9.19)

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

public class PlayerContraller : MonoBehaviour
{
    public Rigidbody2D rbody;

    public float speed = 5f;

    Vector2 currentInput;


    void Start()
    {
        rbody.GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        float _x = Input.GetAxis("Horizontal");
        float _y = Input.GetAxis("Vertical");

        Vector2 movement = new Vector2(_x, _y);

        currentInput = movement;
    }

    private void FixedUpdate()
    {
        Vector2 position = rbody.position;
        position += currentInput * speed * Time.deltaTime;
        rbody.MovePosition(position);
    }
}
View Code

 

推荐阅读