Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Flip trigger based on distance

Discussion in 'Scripting' started by Corva-Nocta, Oct 4, 2020.

  1. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    Hey all, I have one last step to my rotation mechanic that I need ironed out. It should be a simple one but I've been doing code for hours so my brain is a little mush at the moment. Its simple, when my player enters the trigger area I check the distance of the player to the center of the trigger (ignoring the y) and when the player hits the center of the trigger area I rotate the player. When working with just that I have no issues.

    Now, I added in a way that the player can go back through the trigger after being rotated to rotate back to the starting direction. Here is where the issues start to come up. Since I am using effectively a distance check while the player is inside the trigger, it doesn't turn off after my player rotates. So the code bounces back and forth between the new and old direction extremely quickly.

    What I think I need is a way to fire the turning code once until the player gets far enough away from the distance check that it can be reset. Or maybe there is some better way to be doing this that I don't know about. If anyone knows anything that can help please feel free to share!

    ALso, my code:
    Code (csharp):
    1.     public float enterAngle;
    2.     public float exitAngle;
    3.  
    4.     private void OnTriggerStay(Collider other)
    5.     {
    6.         if (other.tag == "Player")
    7.         {
    8.             Movement player = other.GetComponent<Movement>();
    9.             var vectorToTarget = other.transform.position - transform.position;
    10.             vectorToTarget.y = 0;
    11.             var distanceToTarget = vectorToTarget.magnitude;
    12.             if (distanceToTarget < 0.1)
    13.             {
    14.                 if (player.currentAngle == enterAngle)
    15.                 {
    16.                     player.RotatePlayer(transform.position.x, transform.position.z, exitAngle);
    17.                 }
    18.                 else
    19.                 {
    20.                     if (player.currentAngle == exitAngle)
    21.                     {
    22.                         player.RotatePlayer(transform.position.x, transform.position.z, enterAngle);
    23.                     }
    24.                 }
    25.             }
    26.         }
    27.     }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,890
    Why not just use a smaller trigger collider and do your flipping in OnTriggerEnter/OnTriggerExit?
     
  3. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    I considered that but I need the player to rotate on a point in the middle of the trigger, rather than at the edge of the "wall" of the trigger. Basically I want to set the center of the trigger as a pivot point

    I feel like it should be a relatively simple flip to do, but my mind can't quite find the answer. I think I'm missing something simple
     
  4. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    I might be able to get away with nudging the player a little forward after the rotation. It doesn't have to be pixel perfect for that so I can maybe do a small Transform.Translate or something
     
  5. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    I did a basic:
    Code (csharp):
    1. transform.Translate(Vector3.forward);
    which works, but moves him a little far if I'm running through the trigger. just need to limit the movement to like 0.25 forward and I should be good. Not the best fix I was hoping for, but it "works" for now

    Anyone know a simple way to move forward 0.2 units? I'll make a new thread since its a new topic
     
    Last edited: Oct 4, 2020