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! Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class PeopleShuffling : MonoBehaviour { public Transform startPosition; public Transform endPosition; public float speed = 1.0F; private float startTime; private float journeyLength; private bool playerNear = false; void Start() { //I set startTime to the first second fo the game //same as milli in processing startTime = Time.time; //I set a float equal to the distance between the starting point and the ending point journeyLength = Vector2.Distance(startPosition.position, endPosition.position); } //update every frame void Update() { shuffle(); } void shuffle () { if (playerNear == true){ //the distanced covered equals the time since start * the speed; //think distance = rate * time //this is occuring every frame, so this variable constantly changes as the object moves //unlike journeylength, which is a constant number between a and b float distCovered = (Time.time - startTime) * speed; //distance coverered as the ball moves divided by the total distance //is a fraction of the journey float fracJourney = distCovered / journeyLength; //move the ball between the startposition and the end position by the fraction that we calculated above transform.position = Vector2.Lerp(startPosition.position, endPosition.position, fracJourney); } } public void distanceMath (float playerPosX, float playerPosY) { Vector2 player = new Vector2 (playerPosX, playerPosY); float checkLength = Vector2.Distance(player, startPosition.position); print ("Player is " + checkLength + " units from " + gameObject.name); } void isPlayerNear (float distanceFromPlayer) { //Debug.Log ("The x position is " + playerPosX); //Debug.Log ("The y position is" + playerPosY); } } Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerMovement : MonoBehaviour { public float vel; float posX; float posY; public PeopleShuffling peopleShuffling; void Update() { movement (); markPosition (); } void movement () { float moveHorizontal = Input.GetAxis("Horizontal"); float moveVertical = Input.GetAxis("Vertical"); Vector2 movement = new Vector2 (moveHorizontal, moveVertical); GetComponent <Rigidbody2D>().velocity = movement * vel; } void markPosition () { posX = GetComponent <Transform>().position.x; posY = GetComponent <Transform>().position.y; peopleShuffling.distanceMath(posX, posY); } }
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?