Search Unity

Question Dropping down trash cans

Discussion in 'Scripting' started by Skorrth, Jan 10, 2023.

  1. Skorrth

    Skorrth

    Joined:
    Dec 22, 2022
    Posts:
    3
    Hi everyone i just started game developing and need help. I am making 2D Roguelike game and there are trash cans in the game. I want the trash cans to fall to either left or right side when i use my dash skill but the thing is with the code i used the can is faling randomly. I want to make it so when i use dash from left to right the can is gonna fall right and when i use it from right to left the can is gonna fall to left. I gave the code below so if you can help me out i'd really appreciate it.

    Code (CSharp):
    1. public class Breakable : MonoBehaviour
    2. {
    3.     public GameObject[] brokenPieces;
    4.  
    5.  
    6.     // Start is called before the first frame update
    7.     void Start()
    8.     {
    9.        
    10.     }
    11.  
    12.     // Update is called once per frame
    13.     void Update()
    14.     {
    15.        
    16.     }
    17.  
    18.     private void OnTriggerEnter2D(Collider2D other)
    19.     {
    20.         if(other.tag == "Player")
    21.         {
    22.           if(PlayerController.instance.dashCounter > 0)
    23.            {
    24.              Destroy(gameObject);
    25.  
    26.              int randomPiece = Random.Range(0, brokenPieces.Length);
    27.  
    28.              Instantiate(brokenPieces[randomPiece], transform.position, transform.rotation);
    29.            }
    30.         }
    31.     }
    32. }
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,433
    That code doesn't make the can fall, it just instantiates pieces.

    If your "Player"-tagged object has a Rigidbody2D component, you could get the current velocity to see which way the player is dashing.

    Something like (untested):

    Code (csharp):
    1. float direction = 0f;
    2. Rigidbody2D body = other.GetComponent<Rigidbody2D>();
    3. if (body != null)
    4.     direction = Mathf.Sign(body.velocity.x);
    5. if (direction == 0f)
    6.     direction = Mathf.Sign(UnityEngine.Random.Range(-1f, +1f));
    7.  
    This would give you a -1 for left, +1 for right, and a very very small chance of 0 for up/down/motionless.
     
  3. Skorrth

    Skorrth

    Joined:
    Dec 22, 2022
    Posts:
    3

    Hi, i tried the code but it gave some errors, could you give me a more apparent idea about it and also is there a way we can make this 4 directions with up and down as well. I made the designs for that, only thing remaining is the code. Sorry if im being hard at understanding but like i said its just the beginnig for me so i thought it would be best to ask the community about this.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    How are you dashing? Are you using physics properly?

    With Physics (or Physics2D), never manipulate the Transform directly. If you manipulate the Transform directly, you are bypassing the physics system and you can reasonably expect glitching and missed collisions and other physics mayhem.

    Always use the .MovePosition() and .MoveRotation() methods on the Rigidbody (or Rigidbody2D) instance in order to move or rotate things. Doing this keeps the physics system informed about what is going on.

    https://forum.unity.com/threads/col...-unity-physic-rigidbody.1216875/#post-7763061

    https://forum.unity.com/threads/oncollisionenter2d-not-being-called.1266563/#post-8044121
     
  5. Skorrth

    Skorrth

    Joined:
    Dec 22, 2022
    Posts:
    3
    nah its not that deep, i just need the image of fallen trash can to be seen left or right correctly only thing i need is the code because its not a physics problem, just gotta get some code so that it makes the falling image correctly. Like i said im new and not very good at writing new codes, i usually look at some youtube videos and stuff but this time i couldnt fina a solution on google.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    I'm not sure what you mean by this... but... at the end of the day it will be a structure like:

    Code (csharp):
    1. if (<condition here for facing right>)
    2. {
    3.   // code here to make it face right
    4. }
    5. if (<condition here for facing left>)
    6. {
    7.   // code here to make it face right
    8. }
    9.