Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Nominations have been announced for this years Unity Awards. Celebrate the wonderful projects made by your peers this year and get voting! Vote here!
    Dismiss Notice
  5. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  6. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

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:
    22

    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. SparrowsNest

    SparrowsNest

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