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

Vecter3.distance is giving me 0

Discussion in 'Scripting' started by wayne1512, Apr 6, 2018.

  1. wayne1512

    wayne1512

    Joined:
    Oct 23, 2017
    Posts:
    84
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Recorder : MonoBehaviour {
    6.  
    7.     public MainController MainCont;
    8.  
    9.     public bool hasStopped;
    10.  
    11.  
    12.     public List<Record> Records = new List<Record>();
    13.     public Record CurrentRecord = new Record();
    14.  
    15.     void FixedUpdate () {
    16.         if (MainCont.isRecording == true) {
    17.             Record ();
    18.         }
    19.  
    20.  
    21.         if (MainCont.isRecording == true && Records.Count >= 2 ) {
    22.  
    23.             if (((Vector3.Distance (Records [0].pos, Records [1].pos) <= 0) && (Vector3.Distance (Records [0].rot, Records [1].rot) <= 0))) {
    24.                 hasStopped = true;
    25.                 Debug.Log ("Stopped" + Vector3.Distance (Records [0].pos, Records [1].pos) + " : " + (Vector3.Distance (Records [0].rot, Records [1].rot)));              //problem is here
    26.             } else {
    27.                 hasStopped = false;  
    28.             }
    29.         }
    30.  
    31.  
    32.  
    33.         if (MainCont.isWatchingReplay == true) {
    34.             Replay ();
    35.         }
    36.     }
    37.  
    38.     void Record () {
    39.         CurrentRecord.pos = new Vector3 (0, 0, 0);
    40.         CurrentRecord.rot = new Vector3 (0, 0, 0);
    41.  
    42.         CurrentRecord.pos = gameObject.transform.localPosition;
    43.         CurrentRecord.rot = gameObject.transform.localRotation.eulerAngles;
    44.  
    45.         Records.Insert(0,CurrentRecord);
    46.  
    47.     }
    48.  
    49.     void Replay () {
    50.      
    51.     }
    52.  
    53. }
    54.  
    This code is attached to the ball in a bowling game and the code near line 25 is supposed to change a variable whenever the ball stops moving. However whenever the MainCont.isRecording the hasStopped variable immediately turns to true. I tried outputting the distances myself (line 25) but the output is "0 : 0"
    so it looks like the problem is in the vector3.distace not in the if statement

    Please tell me if i'm using the vecter3.distance wrong or another way to do it.

    Edit: Forgot to say. Yes,the ball is moving so distance 0 is incorrect
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,386
    The conditions literally stipulate that both values are zero.
     
  3. wayne1512

    wayne1512

    Joined:
    Oct 23, 2017
    Posts:
    84
    but how are they 0?...
    did you take a look at the record function?
     
  4. Cynikal

    Cynikal

    Joined:
    Oct 29, 2012
    Posts:
    122
    You're using localposition which if it's parented and it's position within the parent is 0,0,0, then this would be accurate.

    Try using just position.
     
  5. wayne1512

    wayne1512

    Joined:
    Oct 23, 2017
    Posts:
    84
    The ball is parented... but its parent is stationary and the script isnt on the parent. The parent is only used to set localpos as 0 0 0 when i need to reset