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

How to make object use gravity if its not in raycast anymore?

Discussion in 'Scripting' started by Serveladik, Nov 5, 2019.

  1. Serveladik

    Serveladik

    Joined:
    Apr 19, 2017
    Posts:
    13
    Cant made the object do smthing after its not in raycast anymore
    Code (CSharp):
    1. if(Physics.Raycast (camera.transform.position, fwd, out raycast, distance) & raycast.transform.tag == "Object")
    2.             {
    3.                 if(raycast.rigidbody)
    4.                 {
    5.  
    6.                     PlayerPrefs.SetString("PulledObj", this.raycast.transform.name);
    7.                     if(Input.GetMouseButton(1))
    8.                     {              
    9.                         this.gravObj.GetComponent<Rigidbody>().AddForce((camera.transform.position-raycast.transform.position).normalized*pullForce/50,ForceMode.VelocityChange);
    10.                         if(raycast.distance<stopDistance)
    11.                         {
    12.                             this.raycast.transform.GetComponent<Rigidbody>().velocity = Vector3.zero;
    13.                             this.raycast.transform.GetComponent<Rigidbody>().Sleep();
    14.                             this.raycast.transform.GetComponent<Rigidbody>().useGravity=false;
    15.                             this.raycast.transform.SetParent(gravyGun.transform);
    16.                             this.raycast.transform.rotation = Quaternion.Slerp(raycast.transform.rotation, Quaternion.LookRotation(camera.transform.position - raycast.transform.position), 100f * Time.deltaTime);
    17.                             click=2;
    18.                         }
    19.                     }
    20.              
    21.                 else if(raycast.rigidbody==null)
    22.             {
    23.                     this.gravObj.transform.GetComponent<Rigidbody>().useGravity=true;
    24.                     this.gravObj.transform.parent=null;
    25.                     click=1;
    26.             }                                
    27.             }
     
  2. Serveladik

    Serveladik

    Joined:
    Apr 19, 2017
    Posts:
    13