Search Unity

How do i detect if im leaning on left wall or right wall ?

Discussion in 'Scripting' started by keshav2010, Dec 6, 2018.

  1. keshav2010

    keshav2010

    Joined:
    Jun 6, 2016
    Posts:
    34

    image
    [link : https://drive.google.com/file/d/1kIdQTwpMY929f1ag2x3u1xLc0xwV1NUo/view?usp=sharing]
    Hello everyone, i will give you a small description of what this picture show before asking doubt
    1. The 3 dots near feet are representing position (origin) of raycasts that are emitted in Vector2.down direction to detect ground

    2. The big RED DOT on top is yet another point that represents position (transform) , its not relevant to my question so just ignore it

    3. The green Circle and box surrounding the sprite is collider for collisions.

    The picture also shows a flipped version of player, facing left and also how the player is organised in my game
    The doubt however is,
    if you notice any one of the 3 dots near feet , say the blue one, notice that when i flipped/rotated the entire player object around Y Axis, the dots too flipped and changed their position, for ground detection this flip doesn't matter because they are always pointing downward

    but, i also want to have 2 more dots near left & right which i want to use to emit rays in left and right direction respectively to detect walls, now ofcourse the issue is, if these 2 dots will flip along with player object, both rays will be pointing inward, toward center and never outside of collision box so no collision detection will take place, how do i solve this problem ?

    I'm trying to avoid trigger colliders, although they will simply solve the problem but few threads on internet suggest it would be bad for performance of my game, I'm making quite a physics heavy game so i would expect a raycast based solution, but i will be waiting for any type of solution and not strictly raycast based.
     
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    1) find the closest wall
    2) find dot product = tells you what way relative to it you are
    3) profit.
     
    keshav2010 likes this.