- Home /
How do I detect changes in my vector2 positions?
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<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);
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<SpriteRenderer>().enabled = false;
Player.GetComponent<BoxCollider2D>().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.
Post the rest of the code here. It might be an issue on whats happening in the rest of the code because the current looks fine.
I'm confused, why wouldn't you just drain the sta$$anonymous$$a in the same chunk of code that ACTUALLY moves the player?
I did it just needs to check if the the player object is moving or not.
Answer by Arcsharp · Mar 24, 2019 at 07:25 AM
Not sure if anyone still cares about this question but I figured out the answer on my own. Just put StartCoroutine(WhateverYourIEnumeratorFunctionNameIs()); in Update so it runs every frame.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
My objects start flying after I Instantiate them 0 Answers
Help in writing a tricky if statement 1 Answer
Vector2.moveTowards but only on one axis 1 Answer
Why does this code not work? 1 Answer