Realize the movement of objects--control position movement in rigid body and code

realize the movement of objects

Let the object have a collision volume first

Bind a rigidbody to an object

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-ZECQOvm5-1676354680561)(C:/Users/86188/AppData/Roaming/Typora/typora-user-images/ image-20230212092520374.png)]

Other attributes and usage methods can be viewed in the documentation, which is really super detailed.

But collision rules alone are not enough, we need to add a rigid body to it.

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-ygsKYeBp-1676354680562) (C:/Users/86188/AppData/Roaming/Typora/typora-user-images/ image-20230212094600407.png)]

Bind the corresponding function:

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-ItvYxx4W-1676354680562) (C:/Users/86188/AppData/Roaming/Typora/typora-user-images/ image-20230214091433207.png)]

Then we write the code. The specific idea is to design the jumping distance of the frog, then calculate the destination position, and then change the position of the rigid body on it.

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

public class PlayController : MonoBehaviour
{
    
    
   
   //用于记录绑定在对象上面的刚体
   private Rigidbody2D rb;

   //可以被外部设置的参数,表示每次跳的距离量
   public float jumpDistance;

   //不可被外部调用的变量,表示实际跳跃的距离】
   private float moveDistance;

   //判断玩家是否已经满足对应的长按操作要求
   private bool buttonHeld;

   //最后位置
   private Vector2 destination;

   //判断当前是否处于跳跃状态
   private bool isJump;

   //钩子函数,比start更早
   private void Awake()
    {
    
    
      //把对象上面的刚体赋值给创建好的刚体对象
      rb = GetComponent<Rigidbody2D>();
   } 

   private void Update() {
    
    
      //判断是否跳完
      if(destination.y - transform.position.y < 0.1f)
      {
    
    
       isJump = false;
      }
   }

   //周期函数,每0.02秒执行一次,判断对象位置是否已经以为我们的操作引起改变
   private void FixedUpdate() {
    
    
        //Vector2.Lerp(当前位置, 最终位置, 移动幅度)
      if(isJump)
      {
    
    
         rb.position = Vector2.Lerp(transform.position, destination, 0.134f);
      }
   }

   //短跳函数
   public void Jump(InputAction.CallbackContext context)
   {
    
    
      
      //TODO:这个todo是标记我们要在这个函数里做什么,除此之外没什么蛋用。

      //如果玩家已经达到操作触发要求,那么打印 “起跳!!!!!”
      // if(context.phase == InputActionPhase.Performed)等同于下面的
      if(context.performed && !isJump)
      {
    
    
         moveDistance = jumpDistance;
         // Debug.Log("起跳!!!!!" + "点击跳跃跳了" + moveDistance + "米远。");
         //计算此时的位置
         destination = new Vector2(transform.position.x, transform.position.y + moveDistance);
         isJump = true;
      } 

   }
   
   //长跳函数
    public void LongJump(InputAction.CallbackContext context)
    {
    
    
   
      if(context.performed && !isJump) 
      {
    
    
         buttonHeld = true;
      }

      //如果玩家已经松开了按键   
      if(context.canceled && buttonHeld == true && !isJump)
      {
    
    
         //计算距离
         moveDistance = jumpDistance * 2;
         // Debug.Log("起跳!!!!!" + "点击跳跃跳了" + moveDistance + "米远。");
         //计算此时的位置
         destination = new Vector2(transform.position.x, transform.position.y + moveDistance);
         isJump = true;
         
         //还原
         buttonHeld = false;
      }
   
    }

   
}

Guess you like

Origin blog.csdn.net/abaidaye/article/details/129025845