Search Unity

Distance Between Two Sprites Bug

Discussion in 'Scripting' started by Carter0, Dec 2, 2016.

  1. Carter0

    Carter0

    Joined:
    Feb 12, 2016
    Posts:
    62
    I am trying to make a game where if the black dot gets near the white dot, then the white dot shuffles back and forth strangely. I have an odd bug however and I can't seem to figure out whats wrong. I am using lerping in order to make the shuffling animation, but it does not seem to register the correct distance between the black dot and the white dot.

    Ill put a screenshot down below which should explain the problem simply, but basically the issue is that my code is not calculating the distance between my dots properly. The closer the black dot gets to the left most white dot, the smaller the number in the console log should be. But the number gets smaller is some distant location instead.

    Ill put my relevant code below the screenshot.

    Thanks for the help!

    Screen Shot 2016-12-02 at 1.36.15 PM.png Screen Shot 2016-12-02 at 1.35.58 PM.png





    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PeopleShuffling : MonoBehaviour {
    6.     public Transform startPosition;
    7.     public Transform endPosition;
    8.     public float speed = 1.0F;
    9.     private float startTime;
    10.     private float journeyLength;
    11.     private bool playerNear = false;
    12.  
    13.  
    14.  
    15.     void Start() {
    16.         //I set startTime to the first second fo the game
    17.         //same as milli in processing
    18.         startTime = Time.time;
    19.         //I set a float equal to the distance between the starting point and the ending point
    20.         journeyLength = Vector2.Distance(startPosition.position, endPosition.position);
    21.     }
    22.     //update every frame
    23.     void Update() {
    24.         shuffle();
    25.        
    26.  
    27.  
    28.        
    29.     }
    30.  
    31.     void shuffle () {
    32.         if (playerNear == true){
    33.             //the distanced covered equals the time since start * the speed;
    34.             //think distance = rate * time
    35.             //this is occuring every frame, so this variable constantly changes as the object moves
    36.             //unlike journeylength, which is a constant number between a and b
    37.             float distCovered = (Time.time - startTime) * speed;
    38.             //distance coverered as the ball moves divided by the total distance
    39.             //is a fraction of the journey
    40.             float fracJourney = distCovered / journeyLength;
    41.             //move the ball between the startposition and the end position by the fraction that we calculated above
    42.             transform.position = Vector2.Lerp(startPosition.position, endPosition.position, fracJourney);
    43.    
    44.         }
    45.  
    46.     }
    47.  
    48.     public void distanceMath (float playerPosX, float playerPosY) {
    49.  
    50.  
    51.         Vector2 player = new Vector2 (playerPosX, playerPosY);
    52.         float checkLength = Vector2.Distance(player, startPosition.position);
    53.         print ("Player is " + checkLength + " units from " + gameObject.name);
    54.  
    55.        
    56.  
    57.     }
    58.  
    59.     void isPlayerNear (float distanceFromPlayer)  {
    60.  
    61.  
    62.         //Debug.Log ("The x position is " + playerPosX);
    63.         //Debug.Log ("The y position is" + playerPosY);
    64.  
    65.  
    66.  
    67.     }
    68.  
    69. }
    70.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour {
    6.  
    7.     public float vel;
    8.  
    9.     float posX;
    10.     float posY;
    11.  
    12.     public PeopleShuffling peopleShuffling;
    13.  
    14.     void Update()
    15.     {
    16.         movement ();
    17.         markPosition ();
    18.  
    19.        
    20.  
    21.     }
    22.  
    23.     void movement () {
    24.         float moveHorizontal = Input.GetAxis("Horizontal");
    25.         float moveVertical = Input.GetAxis("Vertical");
    26.  
    27.         Vector2 movement = new Vector2 (moveHorizontal, moveVertical);
    28.         GetComponent <Rigidbody2D>().velocity = movement * vel;
    29.  
    30.     }
    31.  
    32.     void markPosition () {
    33.         posX = GetComponent <Transform>().position.x;
    34.         posY = GetComponent <Transform>().position.y;
    35.         peopleShuffling.distanceMath(posX, posY);
    36.  
    37.  
    38.  
    39.  
    40.     }
    41.  
    42.  
    43. }
     
  2. JC_SummitTech

    JC_SummitTech

    Joined:
    Nov 1, 2016
    Posts:
    78
    I don't see you assigning startPosition anywhere, and it is what you check the distance to.
    Why not check with the actual object position?