【unity learn】【Ruby2D】Two sets of modes to control the movement of characters

The first one is to use a more realistic mode for movement. The code contains acceleration, speed, and friction. There will be a certain delay when the character starts to move (after all, it must be accelerated first), and the advantages are practical. This mode can be It is used in games that require acceleration such as racing games.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MyRubyController : MonoBehaviour
{
    [SerializeField]
    //将加速度暴露出来使其可调
    private float _accel = 0.1f;//加速度
    [SerializeField]
    public float _maxSpeed = 5f;//maxSpeed:对速度进行限制
    // Start is called before the first frame update
    Rigidbody2D rb2d;//声明刚体组件
    //获取用户输入
    float horizontal;//获取水平键盘输入
    float vertical;//获取垂直键盘输入
    public float friction = 1f;//摩擦力
    void Start()
    {
        //获取当前游戏对象的刚体组件
        rb2d = GetComponent<Rigidbody2D>();
    }
    // Update is called once per frame
    void Update()
    {
        horizontal = Input.GetAxis("Horizontal");
        vertical = Input.GetAxis("Vertical");
        float dt = Time.deltaTime;//时间
        ApplyFriction(dt);
        ApplyMovement(dt);
    }
    private void ApplyMovement(float dt)
    {
        Vector2 accel = new Vector2(horizontal * this._accel, vertical * this._accel);//建立一个加速度向量
        Vector2 vel = rb2d.velocity;//建立一个向量获取当前刚体组件的速度
        vel += accel * dt;//每一帧速度加等于加速度
        float velSize = vel.magnitude;//向量长度
        if (velSize > _maxSpeed)//大于最大速度时置为最大速度
        {
            vel = vel.normalized * _maxSpeed;
        }
        rb2d.velocity = vel;//将修改过的值返回该速度
    }

    private void ApplyFriction(float dt)
    {
        float faccel = friction; //摩擦力加速度
        Vector2 vel = rb2d.velocity; // 人物速度
        vel = Vector2.MoveTowards(vel, Vector2.zero, faccel * dt);//向zero靠近,慢慢减速
        rb2d.velocity = vel;
    }
}

The second set is to start immediately, because the principle is to directly modify the position property of the Rigidbody, but we also need to add a friction system in this set, otherwise we will not be able to stop if we are hit by a rigid body with speed.

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

public class RubyController1 : MonoBehaviour
{
    //将速度暴露出来使其可调
    public float speed = 0.1f;
    int currentHealth;//生命值
    // Start is called before the first frame update
    Rigidbody2D rb2d;//声明刚体组件
    //获取用户输入
    float horizontal;//获取水平键盘输入
    float vertical;//获取垂直键盘输入
    public float friction = 1f;//摩擦力
    void Start()
    {
        //获取当前游戏对象的刚体组件
        rb2d = GetComponent<Rigidbody2D>();
        currentHealth = maxHealth;
    }
    // Update is called once per frame
    void Update()
    {

    }
    private void FixedUpdate()
    {
        horizontal = Input.GetAxis("Horizontal");
        vertical = Input.GetAxis("Vertical");
        Vector2 position = rb2d.position;//tranform组件,该节点tranform修改x和y
        position.x = position.x + speed * horizontal * Time.deltaTime;
        position.y = position.y + speed * vertical * Time.deltaTime;//Time.deltaTime:时间帧率。
        rb2d.position = position;
        float dt = Time.deltaTime;//时间
        ApplyFriction(dt);
    }



    private void ApplyFriction(float dt)
    {
        float faccel = friction; //摩擦力加速度
        Vector2 vel = rb2d.velocity; // 人物速度
        vel = Vector2.MoveTowards(vel, Vector2.zero, faccel * dt);//向zero靠近
        rb2d.velocity = vel;
        //float velSize = vel.magnitude;
        //float accelSize = faccel * dt;
        //if (accelSize >= velSize)
        //{
        //    vel = Vector2.zero;
        //}
        //else
        //{
        //    vel -= vel.normalized * accelSize;
        //}
    }
}

Ruby

Guess you like

Origin blog.csdn.net/qq_63499305/article/details/129958273