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

3D non Rigidbody/Static (IsTrigger) collision

Discussion in 'Editor & General Support' started by Eistee, Dec 10, 2015.

  1. Eistee

    Eistee

    Joined:
    Nov 9, 2015
    Posts:
    16
    Hello community,

    I hope you can give an hint for my problem. I try to detect an collision between two objects and undo the last movement when touched.

    - One object is (half) static (only instant teleports every 5 Minutes), attached:
    1. Sphere Collider

    - the other object is my Main Camera, attached:
    1. Sphere Collider (Is Trigger = true)
    2. Rigidbody (Is Kinematic = true)
    3. Script (GetKeyState for movement & OnTriggerEnter) [Code below]
    It "works" but the movement is very twitching, because it goes into the collider and back outside until the user notice it, and stop pressing the key :(

    Is there a smooth way how to do that in unity?
    Thanks in advance, I'm going crazy.

    Code (CSharp):
    1. private bool colliderEntered = false;
    2.  
    3. // Check collision
    4.     void OnTriggerEnter(Collider other)
    5.     {
    6.         colliderEntered = true;
    7.     }
    8.     void OnTriggerExit(Collider other)
    9.     {
    10.         colliderEntered = false;
    11.     }
    12.  
    13. //-----------------------------------------------------------
    14.  
    15. void Update()
    16.     {
    17.    if(Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W))
    18.            {
    19.                if (!colliderEntered) // no collision move forward & maybe into the collider!
    20.                {
    21.                    transform.position += transform.forward * speed * Time.deltaTime;
    22.                }
    23.                else // you stuck in the collider move out
    24.                {
    25.                    transform.position -= transform.forward * speed * Time.deltaTime;
    26.                }
    27.                lastMove = LastMove.foreward;
    28.            }
    29.         }
    30. // also left, right, backwards, up and down
    31.    }
     
    Last edited: Dec 10, 2015
  2. CoopReed

    CoopReed

    Joined:
    Nov 17, 2015
    Posts:
    27
    I'm only new to c# but would a smoothing loop do the job?
     
  3. Eistee

    Eistee

    Joined:
    Nov 9, 2015
    Posts:
    16
    Sorry, but I have no idea how a (smooth) loop can help here.