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

Marbles are teleported correctly to the location as needed but stay in the air and do not move

Discussion in 'Scripting' started by aemeny, Apr 28, 2022.

  1. aemeny

    aemeny

    Joined:
    Apr 1, 2022
    Posts:
    5
    The marbles seem to teleport to the location correctly but stays in the air and does not respond to outside forces (as if it's kinematic still).
    The teleportation works when I don't change around the objects kinematic state, but this is needed for me.
    Any Ideas?

    When I drop my marbles outside of the map and it exits my barrier box over the world it works completely fine:
    Code (CSharp):
    1. public class LeaveMap : MonoBehaviour
    2. {
    3.     public Transform SpawnPoint;
    4.     public Transform SpawnPoint2;
    5.     ShootPlayerBall shootPlayerBall;
    6.     public GameObject player;
    7.     void Awake()
    8.     {
    9.         shootPlayerBall = player.GetComponent<ShootPlayerBall>();
    10.     }
    11.     void OnTriggerExit(Collider Collider)
    12.     {
    13.         if (Collider.gameObject.tag == "Marble")
    14.         {
    15.             Rigidbody Ball_physics = Collider.gameObject.GetComponent<Rigidbody>();
    16.             Ball_physics.isKinematic = true;
    17.             Collider.gameObject.transform.position = SpawnPoint.transform.position;
    18.             Ball_physics.isKinematic = false;
    19.             shootPlayerBall.launch = true;
    20.         }
    21.         else if (Collider.gameObject.tag == "TargetMarble")
    22.         {
    23.             Rigidbody Ball_physics = Collider.gameObject.GetComponent<Rigidbody>();
    24.             Ball_physics.isKinematic = true;
    25.             Collider.gameObject.transform.position = SpawnPoint2.transform.position;
    26.             Ball_physics.isKinematic = false;
    27.         }
    28.         else
    29.         {
    30.             Destroy(Collider.gameObject);
    31.         }
    32.     }
    33. }

    --------------
    But when I try to teleport the player marbles after being on the floor for ten seconds it does not work:
    Code (CSharp):
    1.  void Update()
    2.     {
    3.       if (startTimer == true)
    4.         {
    5.             currentTime -= 1 * Time.deltaTime;
    6.         }
    7.       if (currentTime <= 0.0f)
    8.         {
    9.             Rigidbody Ball_physics = this.gameObject.GetComponent<Rigidbody>();
    10.             Ball_physics.isKinematic = true;
    11.             this.transform.position = SpawnPoint.transform.position;
    12.             Ball_physics.isKinematic = false;
    13.             currentTime = startingTime;
    14.             startTimer = false;
    15.             ShootPlayerBall refScript = GetComponent<ShootPlayerBall>();
    16.             GetComponent<ShootPlayerBall>().launch = true;
    17.         }
    18.     }

    ---------------
    Or when I try to teleport the target marbles when in contact with the player:

    Code (CSharp):
    1. public class SpawnBallsInCollectionBox : MonoBehaviour
    2. {
    3.     public Transform SpawnPoint;
    4.     void OnCollisionEnter(Collision move_ball)
    5.     {
    6.         if (move_ball.gameObject.tag == "Marble")
    7.         {
    8.             Rigidbody Ball_physics = this.gameObject.GetComponent<Rigidbody>();
    9.             Ball_physics.isKinematic = true;
    10.             this.transform.position = SpawnPoint.transform.position;
    11.             Ball_physics.isKinematic = false;
    12.         }
    13.     }
    14. }

     
    Last edited: Apr 28, 2022
  2. aemeny

    aemeny

    Joined:
    Apr 1, 2022
    Posts:
    5
    note: I'm rather new to unity so don't know everything.
    Also my object that it is being teleported too does not have a box collider ticked but has Is Triggered ticked
     
  3. Peeling

    Peeling

    Joined:
    Nov 10, 2013
    Posts:
    404
    Hi :) Can you possibly edit your post and use the <> Code button to put the code inside code tags? Makes it readable.
     
    aemeny likes this.
  4. aemeny

    aemeny

    Joined:
    Apr 1, 2022
    Posts:
    5
    okay thank you! havent used this before
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,945
    If left alone for a short time, Rigidbodies fall asleep. Are your marbles asleep?

    You can set their sleep mode in code to never fall asleep.
     
  6. Peeling

    Peeling

    Joined:
    Nov 10, 2013
    Posts:
    404
    EDITED: Removed bad advice!
     
    Last edited: Apr 29, 2022
  7. aemeny

    aemeny

    Joined:
    Apr 1, 2022
    Posts:
    5
    Hey, sorry as I am new to unity and don't fully understand everything.

    My "currentTime <= 0.0f" does end up stopping as I reset the timer back to 10 after the teleportation is done.

    Would you suggest I use coroutine for the teleportation even when I will use this teleportation code multiple times.

    How would I go about teleporting the rigidbodys position instead of the transform I couldn't find anything about it online. Also I am turning kinematic on then off to stop any forces that were applied to the ball as it was rolling beform being moved and continues when moved.

    thanks.
     
  8. Peeling

    Peeling

    Joined:
    Nov 10, 2013
    Posts:
    404
    Sorry; I missed that line resetting the countdown timer when I read through.

    Have you tried adding logging to find out which part isn't working? EG does the callback for collision with the player get called? Does the timeout actually happen? Does the countdown actually happen? Does the teleportation stop happening?
     
  9. aemeny

    aemeny

    Joined:
    Apr 1, 2022
    Posts:
    5
    Hey went through and used a bunch of Debug.Logs and everything only teleports once and counts down correctly