Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Box collider 2d not colliding continuously

Discussion in 'Scripting' started by nicklowkc1, Nov 30, 2019.

  1. nicklowkc1

    nicklowkc1

    Joined:
    Feb 27, 2019
    Posts:
    28
    I am making a top down rpg game. There are two game objects which is my character and a treasure box. Both have box collider 2d attached to them. The problem I am having is OnCollisionStay2D only happened if I keep pressing right, even though the colliders are colliding with each other. I expected it to stay colliding. Below is my code for my character:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6.  
    7.     [SerializeField]
    8.     private float speed;
    9.     private Vector2 playerVector;
    10.     private Animator animator;
    11.     private Rigidbody2D myRigidBody;
    12.     private Vector2 facingLeft = new Vector2(-0.5f, 0.5f);
    13.     private Vector2 facingRight = new Vector2(0.5f, 0.5f);
    14.  
    15.     CreateScreenFade blackFade = null;
    16.  
    17.     [SerializeField]
    18.     private List<Vector2> firstStagePositionList;
    19.  
    20.     [SerializeField]
    21.     PositionManager positionManager;
    22.  
    23.     private int currentRoom = 6;
    24.     private bool hasKey = false;
    25.     public bool HasKey { set { hasKey = value; } }
    26.     public int CurrentRoom
    27.     {
    28.         get {
    29.             return currentRoom;
    30.         }
    31.     }
    32.    
    33.  
    34.     // Use this for initialization
    35.     void Start () {
    36.         playerVector = transform.position;
    37.         animator = GetComponent<Animator>();
    38.         myRigidBody = GetComponent<Rigidbody2D>();
    39.         blackFade = GameObject.Find("CreateBlackScreen").GetComponent<CreateScreenFade>();      
    40.     }
    41.    
    42.     // Update is called once per frame
    43.     void Update () {      
    44.         PlayerMovingLogic();
    45.     }
    46.  
    47.     private void PlayerMovingLogic( )
    48.     {
    49.         if(!CreateScreenFade.isFading) {          
    50.             playerVector.x = Input.GetAxisRaw("Horizontal");
    51.             playerVector.y = Input.GetAxisRaw("Vertical");
    52.            
    53.             //Player animation logic
    54.             if(playerVector.x > 0.0f || playerVector.x < 0.0f || playerVector.y >0.0f || playerVector.y    < 0.0f) {
    55.                 animator.SetBool("Run", true);
    56.             } else {
    57.                 animator.SetBool("Run", false);
    58.             }
    59.  
    60.             //Player Direction logic
    61.             if(playerVector.x  > 0.0f) {
    62.                 transform.localScale = facingRight;
    63.             }else if (playerVector.x  < 0.0f) {
    64.                 transform.localScale = facingLeft;
    65.             }          
    66.         } else {
    67.             animator.SetBool("Run", false);
    68.         }
    69.     }
    70.  
    71.     private void MoveCharacter() {
    72.         //myRigidBody.MovePosition(transform.position + playerVector * speed * Time.deltaTime);      
    73.     }
    74.  
    75.     private void FixedUpdate( )
    76.     {
    77.         myRigidBody.MovePosition(myRigidBody.position + playerVector * speed * Time.fixedDeltaTime);
    78.     }
    79.  
    And also treasure chest code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TreasureChest : MonoBehaviour
    6. {
    7.     [SerializeField] GameObject spawnObject;
    8.     private Animator animator;
    9.     private bool isOpened = false;
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.         animator = GetComponent<Animator>();
    14.     }
    15.  
    16.     private void OnCollisionStay2D(Collision2D collision)
    17.     {
    18.         if(collision.gameObject.tag == "Player") {
    19.             Debug.Log("Collide with chest");
    20.             if(Input.GetKeyDown(KeyCode.Space) && !isOpened ) {
    21.                 isOpened = true;
    22.                 animator.SetBool("Open", true);
    23.                 Instantiate(spawnObject, transform.position, Quaternion.identity);
    24.             }
    25.         }
    26.     }
    27.  
    28. }
    29.  
    Here is the rigidbody2d settings for both




    Thank you.
     
  2. nicklowkc1

    nicklowkc1

    Joined:
    Feb 27, 2019
    Posts:
    28
    I don

    I dont know why the image is not working.

    https://imgur.com/1JdBtuJ
    https://imgur.com/G3XVClc