Record the pits encountered in personal learning unity (1)-2D characters hit the wall and twitch around

The code that has the problem is as follows:

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

public class PlayController : MonoBehaviour
{
    
    
    [Header("移动参数")]
    public Vector2 currentVelocity;
    public float xInput;
    public float LRMoveForce;
   private void Start()
    {
    
    
        pos = GetComponent<Transform>();
        rb = GetComponent<Rigidbody2D>();
    }
    private void Update()
    {
    
    
        LRMovement();
    }

  private void LRMovement()
    {
    
    
        if (rb.velocity.x < 0)
        {
    
    
            faceDir = -1;
            pos.rotation = Quaternion.Euler(0, 180, 0);
            //transform.localScale = new Vector3(-1, 1, 1);

        }
        if (rb.velocity.x > 0)
        {
    
    
            faceDir = 1;
            pos.rotation = Quaternion.Euler(0, 0, 0);
            //transform.localScale = new Vector3(1, 1, 1);
        }

        xInput = Input.GetAxisRaw("Horizontal");      
        rb.velocity = new Vector2(xInput * LRMoveForce, rb.velocity.y);
        currentVelocity = rb.velocity;
    }
}

The script is mounted on the player. When the player encounters a collider of a static rigid body in the horizontal direction, continuously pressing the move button may cause the character to twitch from side to side. At the same time, it is observed that position.x will jitter in a very small range, and potion.y will decrease at a very small rate.

Either changing the method of flipping the object from left to right to modifying the loaclscale, or changing the method of moving the object from modifying the velocity to addforce can not solve the problem.

A feasible solution: the code for judging the orientation of the object is not placed under the judgment of rb.velocity.x, the modified code is as follows

   private void LRMovement()
    {
    
    
        if (rb.velocity.x < 0)
        {
    
    
            faceDir = -1;
            //pos.rotation = Quaternion.Euler(0, 180, 0);
            //transform.localScale = new Vector3(-1, 1, 1);

        }
        if (rb.velocity.x > 0)
        {
    
    
            faceDir = 1;
            //pos.rotation = Quaternion.Euler(0, 0, 0);
            //transform.localScale = new Vector3(1, 1, 1);
        }

        xInput = Input.GetAxisRaw("Horizontal");
        if (xInput < 0)
        {
    
    
            pos.rotation = Quaternion.Euler(0, 180, 0);
        }
        if (xInput > 0)
        {
    
    
            pos.rotation = Quaternion.Euler(0, 0, 0);
        }
        rb.velocity = new Vector2(xInput * LRMoveForce, rb.velocity.y);
        currentVelocity = rb.velocity;
    }

(The first time I tried to make a game, I was not very experienced. If there is a better method or know the reason, I hope you can point it out in the comment section.)

Guess you like

Origin blog.csdn.net/wsWind/article/details/108966363