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

How can I calculate the distance from the floor on specific floor ?

Discussion in 'Scripting' started by Chocolade, Apr 12, 2018.

  1. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    916
    I have many objects that are Floor in the Hierarchy. So I searched in the Hierarchy for all the Floors and tagged them all as Floor.

    Now in the script I want to make it global script so I can use it in many places/cases.

    In this case I'm in a room and the transform the script is attached to have a Rigidbody component and the transform just fall down. Before I used OnCollisionEnter to find when the transform hit the floor. But now I want to calculate the distance between the transform and floor and to decide at what distance to stop the falling. For example at distance 1 or distance 3 or maybe distance 7.

    So I'm using ground array to find all the Floors and then trying to calculate the distance:

    Code (csharp):
    1.  
    2. distance = Vector3.Distance(transform.position, ground.transform.position);
    3.             if (distance > 1)
    4.  
    But the ground is array. So how can I find the current floor I'm standing on ?
    Should I loop over all the Floors and calculate the distances ?

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class NaviMove : MonoBehaviour
    7. {
    8.     public GameObject target;
    9.     public ObjectsManipulation om;
    10.     public float moveSpeed = 1f;
    11.     public float maxFallSpeed = 2f;
    12.     public float rotationSpeed = 1f;
    13.     public GameObject player;
    14.  
    15.     private Rigidbody rigid;
    16.     private Waypoints waypoints;
    17.     private Camera navicam;
    18.     private GameObject[] ground;
    19.     private float distance;
    20.  
    21.     void Awake()
    22.     {
    23.         rigid = GetComponent<Rigidbody>();  //get the rigidbody that attached to the object
    24.     }
    25.     void Start()
    26.     {
    27.         waypoints = GetComponent<Waypoints>();
    28.         navicam = GetComponent<Camera>();
    29.         ground = GameObject.FindGameObjectsWithTag("Floor");
    30.     }
    31.     void FixedUpdate()  //physic should be done in FixedUpdate not Update
    32.     {
    33.         if (DetectInteractable.detected == true && ItemAction.move == true)
    34.         {
    35.             transform.parent = null;  //disconnect the object for it's parent
    36.             om.stopRotation = true;
    37.             rigid.isKinematic = false;
    38.         }
    39.         if (om.stopRotation == true && ItemAction.move == true)
    40.         {
    41.             distance = Vector3.Distance(transform.position, ground.transform.position);
    42.             if (distance > 1)
    43.             {
    44.                 navicam.enabled = true;
    45.                 navicam.transform.rotation = transform.rotation;
    46.                 navicam.transform.forward = transform.forward;
    47.                 if (rigid.velocity.sqrMagnitude > maxFallSpeed) //check if it's falling to fast
    48.                 {
    49.                     rigid.velocity *= 0.01f; // falling to fast, so reduce speed,  you may want to play around with this number.
    50.                 }
    51.                 else // if not falling to fast add force to make it fall faster
    52.                 {
    53.                     rigid.AddForce(Vector3.down * moveSpeed);
    54.                 }
    55.             }
    56.             else
    57.             {
    58.                 ItemAction.move = false;
    59.                 rigid.isKinematic = true;
    60.                 waypoints.enabled = true;
    61.                 player.SetActive(false);
    62.             }
    63.         }
    64.     }
    65.  
    66.     private void OnCollisionEnter(Collision collision)
    67.     {
    68.         if (collision.transform.name.Contains("Floor"))
    69.         {
    70.            
    71.         }
    72.     }
    73. }
    74.  
    Maybe I should stay with the OnCollisionEnter but then how can I calculate the distance and decide from what distance to stop falling ?
     
  2. CubicCBridger

    CubicCBridger

    Joined:
    Apr 19, 2017
    Posts:
    44
  3. alexeu

    alexeu

    Joined:
    Jan 24, 2016
    Posts:
    257
    You could use Physics.Raycast , RaycastHit.distance and a layerMask.