0

I'm making a 2D game where the player moves left and right while dodging falling objects, I put triggers so it doesn't leave the screen but sometimes when it collides with these triggers it passes this collider. I sent an image of the colliders, the Player inspector and also a collider.

Here my Player code

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

public class Ball : MonoBehaviour
{
    public bool isRight;
    public float speed;
    public Transform pointR;
    public Transform pointL;
    Rigidbody2D rb;
    private SpriteRenderer sprite;
    private float maxSpeed;
    
    [Header("PowerUps")]
  public float force;
    
   
    

     
    
    // Start is called before the first frame update
    void Start()
    {
     rb = GetComponent<Rigidbody2D>();
      sprite = GetComponent<SpriteRenderer>();
      maxSpeed = 600f;
     
      
     
     

    }
         void Update()
        {
             if(Input.GetMouseButtonDown(0))
        {
            isRight = !isRight;
         
        }
        }

    void FixedUpdate()
    {
        
        if(isRight)
        {
             rb.velocity = new Vector2(speed, 0);
            rb.AddForce(rb.velocity,ForceMode2D.Force );
             sprite.flipX = true;
             if(rb.velocity.x >= maxSpeed)
             {
                rb.velocity = new Vector2(maxSpeed, 0);
             }

      
        }

        else
        {
            rb.velocity = new Vector2(-speed, 0 );
            rb.AddForce(rb.velocity, ForceMode2D.Force );
            
             
            sprite.flipX = false;
            
        }
    }

  
    void OnTriggerEnter2D(Collider2D collider)
    {

        if(collider.gameObject.tag =="ColliderL")
        {
            
            isRight = !isRight;
        }
          if(collider.gameObject.tag == "ColliderR")
        {
            isRight = !isRight;
        }


        // For PowerUps
        if(collider.gameObject.tag == "SpeedUp")
        {
            StartCoroutine(Seconds());
        }
     }

     IEnumerator Seconds()
     {
        rb.AddForce(rb.velocity * force);
        yield return new WaitForSeconds(5f);
        rb.AddForce(rb.velocity * -force);
     }
     
     

    

}
NIK 444
  • 23
  • 5

1 Answers1

1

I would ideally need to see your scene setup to further pinpoint your mistakes. But these are a few common mistakes that are made:

  • Tags are case-sensitive. Also, make sure to assign them.
  • Your collider on one of the object is not 2D; There is Collider, then there is Collider2D.
  • Your collider is not marked as trigger.

Though there are a few non-related problems with your setup:


rb.velocity.collider doesn't exist. You are looking for rb.velocity.

Ideally you want to set physics changes (velocity, in your case) to FixedUpdate() instead.

If you do want to get colliders from RigidBody, use GetAttachedColliders.


Assuming you are setting up a border which the player can never pass through, you should use a full collider instead of a trigger. This uses OnCollisionEnter2D.


new Vector2(speed, 0* Time.deltaTime);

0 * Time.deltaTime does nothing, since you are multiplying by 0.

What you are looking for is probably new Vector2(speed * Time.deltaTime, 0);. But you don't need to multiply the speed by deltaTime since you are not moving the ball manually via it's Transform.

rb.velocity = new Vector2(speed, 0); should be your result.

WQYeo
  • 3,973
  • 2
  • 17
  • 26
  • I'm using "rb.velocity = new Vector2(speed, 0);" but so I put a collider using the box collider that are the Transforms, but if the Player is too fast it goes beyond this collider – NIK 444 Sep 03 '22 at 13:55