So I’m currently working on a 2d game and I’m trying to detect changes in the vector 2 positions by checking if the x and y startPos axis aren’t equal to the finalPos in order to change a boolean from false to true but I can’t seem to get it to work.
using System.Collections;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public GameObject Player;
private Vector2 mousePosition;
bool Pause = false;
private bool replenishStamina = false;
private bool isMoving = false;
public float staminaDrain = 20f;
public float staminaGain = 20f;
public float moveSpeed = 0.1f;
// Start is called before the first frame update
private void Start()
{
Player.GetComponent<SpriteRenderer>().enabled = false;
Player.GetComponent<BoxCollider2D>().enabled = false;
}
// Update is called once per frame
private void Update()
{
PlayerControl();
StorePosition();
Pauser();
}
//Controls all player function
private void PlayerControl()
{
var play = GameObject.Find("Player");
if (replenishStamina == true && Stamina.stamina < 100)
{
Stamina.stamina += staminaGain * Time.deltaTime;
}
if (Stamina.stamina > 100)
{
Stamina.stamina = 100f;
}
mousePosition = new Vector2(mousePosition.x, mousePosition.y);
PlayerMovement();
PlayerStop();
}
//Actives player and allows movement.
private void PlayerMovement()
{
if (Input.GetMouseButton(0))
{
replenishStamina = false;
Player.GetComponent<SpriteRenderer>().enabled = true;
Player.GetComponent<BoxCollider2D>().enabled = true;
if (Player.GetComponent<SpriteRenderer>().enabled == true && Player.GetComponent<BoxCollider2D>().enabled == true)
{
mousePosition = Input.mousePosition;
mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
transform.position = Vector2.Lerp(transform.position, mousePosition, Time.deltaTime * moveSpeed);
if(Stamina.stamina > 0 && isMoving == true)
{
Stamina.stamina -= staminaDrain * Time.deltaTime;
}
if(Stamina.stamina <= 0)
{
Stamina.stamina = 0f;
Player.GetComponent<SpriteRenderer>().enabled = false;
Player.GetComponent<BoxCollider2D>().enabled = false;
}
}
else
{
Stamina.stamina += staminaGain * Time.deltaTime;
}
}
#if UNITY_ANDROID
foreach (Touch touch in Input.touches)
{
if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled && Stamina.stamina != 0)
{
Player.GetComponent().enabled = true;
Player.GetComponent().enabled = true;
if (Player.GetComponent().enabled == true && Player.GetComponent().enabled == true)
{
mousePosition = Input.mousePosition;
mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
transform.position = Vector2.Lerp(transform.position, mousePosition, Time.deltaTime * moveSpeed);
Stamina.stamina -= staminaDrain * Time.deltaTime;
}
}
}
#endif
}
//Deactivates the player.
private void PlayerStop()
{
if (Input.GetMouseButtonUp(0))
{
replenishStamina = true;
Player.GetComponent<SpriteRenderer>().enabled = false;
Player.GetComponent<BoxCollider2D>().enabled = false;
}
#if UNITY_ANDROID
foreach (Touch touch in Input.touches)
{
if (touch.phase == TouchPhase.Ended && touch.phase == TouchPhase.Canceled)
{
Player.GetComponent().enabled = false;
Player.GetComponent().enabled = false;
}
}
#endif
}
//Tells stamina drain whether player is moving or not.
private IEnumerator StorePosition()
{
Vector2 startPos = Player.transform.position;
yield return new WaitForSeconds(0.1f);
Vector2 finalPos = Player.transform.position;
if (startPos.x != finalPos.x || startPos.y != finalPos.y)
{
isMoving = true;
}
}
//Pauses the game.
private void Pauser ()
{
if (Pause == false)
{
Time.timeScale = 1;
}
else
{
Time.timeScale = 0;
}
if (Input.GetKeyDown(KeyCode.P))
{
if (Pause == true)
{
Pause = false;
}
else
{
Pause = true;
}
}
}
//Disables arrows hit by player.
private void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Arrow"))
{
other.gameObject.SetActive(false);
}
}
}
My assumption is that the finalPos is still equal to startPos but I have no idea how to fix it. I put the method by calling it in Update and its in the same script so I’m not sure what I’m doing wrong. Any help would be much appreciated.