Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

2D Platformer - How to detect object beneath the Player from another object?

Discussion in '2D' started by krakena98, Jul 5, 2019.

  1. krakena98

    krakena98

    Joined:
    Jul 5, 2019
    Posts:
    2
    Hello gamers, I'm new to Unity and I'm having a hard time figuring something out.
    I want my enemy unit to jump on the same platform as the player, so far I've made it jump like so:
    Code (CSharp):
    1. private void Jump()
    2.     {
    3.         float playerX = TargetPlayer.transform.position.x;
    4.         float bossX = rigidBoss.position.x;
    5.         float distance = playerX - bossX;
    6.  
    7.         rigidBoss.velocity = new Vector2(distance / 2, 30);
    8.     }
    This works as a placeholder but it's not good enough and it's not the intended game mechanic.

    These are the problems I'm having a hard time figuring out:
    1. The enemy needs to know what platform (GameObject) the Player is currently standing on.
    2. The enemy needs to jump exactly at the center of the platform.
    3. When the player is below the enemy he needs to disable collision with all other platforms except the one he has to get to.

    Thank you for your time~
     
  2. TigerDawn

    TigerDawn

    Joined:
    Aug 4, 2018
    Posts:
    18
    1. Use a raycast from Player to Platform. Platform should be tagged correctly.

    2. That is a statement, not a question.

    3. That is also a statement.
     
  3. krakena98

    krakena98

    Joined:
    Jul 5, 2019
    Posts:
    2
    Hello and thank you for your response. How could I store the raycast "result"/value?
    Also yes, they are statements... but it's something I want to do which I don't really know how to, could you help me? I have the general idea in my head, I just don't know the unity library and engine that well atm.
     
  4. TigerDawn

    TigerDawn

    Joined:
    Aug 4, 2018
    Posts:
    18
    A raycast will throw an collision event, and in the event handler is where you would store or resolve the collision.

    as to 2 and 3. I am not sure what you are trying to implement. 3 sounds like you just want to do a teleport action, which is new Vector3 and add to the object you want to move. 2 I would have to get a better picture of what you are trying to do. What do you define a platform? How is here getting there? etc.