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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Look at specific point of a collider

Discussion in 'Scripting' started by AMIR_REZAs, Jun 18, 2015.

  1. AMIR_REZAs

    AMIR_REZAs

    Joined:
    Jan 20, 2014
    Posts:
    54
    Hello

    I have a 3d character and a curve edge something like this image:


    Now I want to check every point is in front of the player and change rotation of player according to this point. And also then move right and left.
    I've used rayCast for doing it but I couldn't do it nice.
    What's the best way for doing it?
     
  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,077
    There's various ways of doing it - is your curved edge quite thick or just a 2d curve?

    If you raycast against a surface you get a RaycastHit object back, that contains the normal of the surface you've hit. You can use that normal to rotate the player so it faces the same direction as the curve part you just hit.

    To make it smoother you could raycast where each of the players arms are in your diagram to get 2 hit points - if you create a normal from those it'll be less jerky.
     
  3. AMIR_REZAs

    AMIR_REZAs

    Joined:
    Jan 20, 2014
    Posts:
    54
    It's 3D curve edge. Thanks for your answer what about moving?
     
  4. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    You can cross the hit normal with your viewing direction to get a sideways vector to use for moving.
     
    AMIR_REZAs likes this.
  5. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,077
  6. AMIR_REZAs

    AMIR_REZAs

    Joined:
    Jan 20, 2014
    Posts:
    54
    It doesn't work nice yet. Here is an image of what I did.
    1 - ray forward to the edge
    2- use normal to rotate player
    3- use the method dot to calculate direction
    4- set direction

    my problem is that player move in direction and after a while the ray doesn't work and fall!

     
  7. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,077
    When you hit the wall with the raycast you also need to move the player so it's not intersecting with it. For instance, if the distance between the player and the hit point is (for example) less than 0.2 metres you need to move the player so it's at least 0.2m away from the wall. The kind of thing you want to do might be easier to do with the built in Unity physics? You could set a capsule collider around the player and have a mesh collider on your curved wall - Unity will take care of making sure the player doesn't go through the wall etc...

    Also, when you do a raycast it's probably worth starting it a little bit behind the player in case the player is already intersecting. For example, move the start position by the players negative forward vector multiplied by 0.2m