Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Arcsharp · Mar 15, 2019 at 04:57 PM · transformprogrammingvector2issues

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.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cornelis-de-Jager · Mar 14, 2019 at 04:20 AM 0
Share

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.

avatar image Arcsharp Cornelis-de-Jager · Mar 14, 2019 at 05:19 AM 0
Share

Posted the rest of the code.

avatar image RobAnthem · Mar 14, 2019 at 04:22 AM 0
Share

I'm confused, why wouldn't you just drain the sta$$anonymous$$a in the same chunk of code that ACTUALLY moves the player?

avatar image Arcsharp RobAnthem · Mar 14, 2019 at 05:19 AM 0
Share

I did it just needs to check if the the player object is moving or not.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

139 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges