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. Dismiss Notice

Move up gameobject from ground ?

Discussion in 'Scripting' started by blackmask86, Dec 4, 2020.

  1. blackmask86

    blackmask86

    Joined:
    Apr 22, 2020
    Posts:
    7
    Hello , i have a script for align to coin from ground or some objects..

    Here is a code ;

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class raycast2 : MonoBehaviour
    6. {
    7.      Rigidbody rb;
    8.  
    9.     void Start()
    10.     {
    11.         rb = GetComponent<Rigidbody>();
    12.     }
    13.  
    14.     void FixedUpdate()
    15.     {
    16.         RaycastHit hit;
    17.         if (Physics.Raycast(transform.position, -Vector3.up, out hit))
    18.         {
    19.          
    20.         }
    21.  
    22.         if (hit.distance <= 2.0f)
    23.         {
    24.             rb.velocity = transform.up * 150 * Time.deltaTime;
    25.         }
    26.         else if (hit.distance >= 2.0f)
    27.         {
    28.             rb.velocity = -transform.up * 150 * Time.deltaTime;
    29.         }
    30.     }
    31. }
    32.  
    But i have a problem, i cant set same transform position. It has vibration. what can i do please help me.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    If you want it to snap to the ground, just assign it to the right height above the ground and set the velocity to zero.

    IF you want it to move towards the ground, when it arrives at the ground (either going down to or up from), then stop setting the velocity nonzero. NOTE: it will NEVER be equal to 2.0: you cannot test floating point equality, so you need to sense it going from above to below.

    NOTE: gravity may continue to make it fall. If so, turn off gravity on that Rigidbody.
     
    blackmask86 likes this.
  3. blackmask86

    blackmask86

    Joined:
    Apr 22, 2020
    Posts:
    7

    Thank u for reply, It is true.. But during the game, I instantiate objects that sometimes moving.. Also different in height. When objects are in motion, they have to lift it up instead of passing through gold. After the object comes out from under it, it must return to its old place according to the height of the ground. I recorded video for understand me. Sorry for bad english
     
    Last edited: Dec 5, 2020
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    I'm actually not sure what you're trying to do. I recommend working through some more Youtube tutorials on games that are similar to your game, perhaps that can help you understand the problem better.
     
    blackmask86 likes this.
  5. blackmask86

    blackmask86

    Joined:
    Apr 22, 2020
    Posts:
    7
    Thank you bro