Search Unity

Move object to the specific location

Discussion in 'General Discussion' started by thuiyang, May 14, 2020.

  1. thuiyang

    thuiyang

    Joined:
    Apr 14, 2020
    Posts:
    4
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MoveObject2 : MonoBehaviour
    6. {
    7.     public static int count = 1;
    8.     private Rigidbody rb;
    9.     //Start position
    10.     private Vector3 startPos;
    11.     private Vector3 startPos2;
    12.     private Vector3 endPosUpPos;
    13.     //End Position
    14.     private Vector3 endPosUp;
    15.     private Vector3 endPosDown;
    16.     //Distance
    17.     private float distance = 100f;
    18.  
    19.     // Start is called before the first frame update
    20.     void Start()
    21.     {
    22.         rb = GetComponent<Rigidbody>();
    23.         startPos = new Vector3(0, 0, 0);
    24.         startPos2 = new Vector3(0, 5, 0);
    25.         endPosUp = startPos + Vector3.up * distance;
    26.         endPosUpPos = rb.transform.position;
    27.         endPosDown = endPosUpPos + Vector3.down * distance;
    28.     }
    29.  
    30.     // Update is called once per frame
    31.     void Update()
    32.     {
    33.         if (Input.GetMouseButtonUp(0))
    34.         {
    35.             print(count);
    36.             MoveObj();
    37.             if (count == 0)
    38.             {
    39.                 count = 1;
    40.             }else
    41.             { count = 0; }
    42.         }
    43.        
    44.     }
    45.  
    46.     public void MoveObj()
    47.     {
    48.        
    49.         if (count == 1)
    50.         {
    51.  
    52.             while(rb.transform.position!= startPos2) //try to detect the object position
    53.             {
    54.                 rb.AddForce(transform.up * distance);
    55.             }
    56.            
    57.         }
    58.         else if(count == 0)
    59.         {
    60.             while (rb.transform.position != startPos) //try to detect the object position
    61.             {
    62.                 rb.AddForce(-transform.up * distance);
    63.             }
    64.         }
    65.     }
    66. }
    67.  
    There is no error with this MoveObj code, but after I play, when I click mouse button, Unity program is stuck and not responsive. I know there is something wrong in the while loop. Can anyone help?

    Below attach with some images:
    upload_2020-5-14_12-34-39.png
    upload_2020-5-14_12-35-9.png
     
  2. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,566
    You have an infinite loop:
    Code (csharp):
    1.  
    2. [LIST=1]
    3. [*]while(rb.transform.position!= startPos2) //try to detect the object position
    4. [*]            {
    5. [*]                rb.AddForce(transform.up * distance);
    6. [*]            }
    7. [/LIST]
    8.  
    You can't move rigidbody in current frame by applying force to it. No matter how much force you add, it will not move even one micron in current frame.
    Because force changes acceleration, and acceleration affects velocity, which in turn affects position rigidbody will have in the next frame.Because the body never moves, condition is never satisfied, and you get an infinite loop.

    Additionally, it will never reach startPos2 even if you moved it, because vectors are stored as floating points, and those are not precise. So testing for equality between two vectors is not a very good idea. YOu can test for distance between them, and see if hte distance is less than some sort of threshold.
     
    Last edited: May 14, 2020