Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 samualsock · Mar 02, 2015 at 11:08 AM · mousecontrollerrealtimestrategy

Movement coroutine not working correctly

I have a coroutine setup up for making units in my RTS game move to a right mouse click but it is not working correctly. If I right click too soon after right clicking for the first time the unit will stop and it doesn't move at a consistent rate, it will slow down as it gets closer to the destination.

Ideally I would have the unit move at a consistent rate from it's starting point to the clicked point and have it able to change the destination while it is moving. Below is my current code

 using UnityEngine;
 using System.Collections;
 
 public class UnitControl : MonoBehaviour {
     public GameObject[] Units;
     // Use this for initialization
     void Start () 
     {
 
 
 
 
     }
 
     bool unitMoving = false;
 
     IEnumerator Move(GameObject Unit, Vector3 target) 
     {
         while(Vector3.Distance(transform.position, target)>0.005f) 
         {
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit = new RaycastHit();
             transform.position=Vector3.Lerp(transform.position, target, 3.0f*Time.deltaTime);
             yield return null;
         }
 
         unitMoving = false;
     }
 
     // Update is called once per frame
     void FixedUpdate () 
     {
         float moveSpeed = 5.1f;
         if (Input.GetMouseButtonDown (1)) 
         {
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit = new RaycastHit();
             if (Physics.Raycast (ray, out hit))
             {
                 Vector3 movePos = new Vector3 (hit.point.x, 1.5f, hit.point.z);
                 isSelectedScript isSelectedScript = GetComponent<isSelectedScript>();
                 Debug.Log("Debug working before isSelected loop");
 
                     if (isSelectedScript.isSelected == 1)
                     {
                         Debug.Log("This is between the if and the while loops");
                         transform.LookAt(hit.point);
                         StartCoroutine(Move(gameObject, hit.point));
 //                        while(Vector3.Distance(transform.position, hit.point)>0.005f) 
 //                        {
 //                            transform.position=Vector3.Lerp(transform.position, hit.point, 10.0f*Time.fixedDeltaTime);
 //                        }
 
                     }
 
             }
         }
     }
 }
 

Comment
Add comment · Show 3
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 ThomasVandenberghe · Mar 02, 2015 at 01:12 PM 0
Share

You are moving from the current position to the new position with a certain percentage, but since that distance gets smaller over time, so does the move distance. You have to store the original position and lerp between the original and the target.

To move to a new target if you clicked again, store the new current position, stop the current coroutine and start a new one.

avatar image samualsock · Mar 02, 2015 at 01:39 PM 0
Share

So I need to store the starting position and target posotion as variables and then lerp between those two variables?

avatar image samualsock · Mar 02, 2015 at 07:25 PM 0
Share

Alright, I implemented a stop coroutine command but I'm not sure if it is placed correctly

 using UnityEngine;
 using System.Collections;
 
 public class UnitControl : $$anonymous$$onoBehaviour {
     public GameObject[] Units;
     // Use this for initialization
     void Start () 
     {
 
 
 
 
     }
 
     bool unit$$anonymous$$oving = false;
 
     IEnumerator $$anonymous$$ove(GameObject Unit, Vector3 target) 
     {
         Vector3 startPosition = transform.position;
         while(Vector3.Distance(startPosition, target)>0.005f) 
         {
             transform.position=Vector3.$$anonymous$$oveTowards(transform.position, target, 7*Time.deltaTime);
             yield return null;
         }
     }
 
     // Update is called once per frame
     void FixedUpdate () 
     {
         float moveSpeed = 5.1f;
         if (Input.Get$$anonymous$$ouseButtonDown (1)) 
         {
             StopCoroutine("$$anonymous$$ove");
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit = new RaycastHit();
             if (Physics.Raycast (ray, out hit))
             {
                 Vector3 movePos = new Vector3 (hit.point.x, 1.5f, hit.point.z);
                 isSelectedScript selectedScript = GetComponent<isSelectedScript>();
                 Debug.Log("Debug working before isSelected loop");
 
                     if (selectedScript.isSelected == 1)
                     {
                         Debug.Log("This is between the if and the while loops");
                         transform.LookAt(hit.point);
                         StartCoroutine($$anonymous$$ove(gameObject, hit.point));
                     }
 
             }
         }
     }
 }
 

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by fafase · Mar 02, 2015 at 01:42 PM

Use MoveTowards instead of Lerp. Lerp uses the remaining distance while MoveTowards uses the distance you give as third parameter.

As a result, the movement is constant.

If the distance given is greater than the remaining distance then it returns the target position.

Comment
Add comment · Show 8 · 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
avatar image samualsock · Mar 02, 2015 at 04:50 PM 0
Share

I changed it to move towards and incorporated the suggestion above but now the unit does not move. This is the new coroutine

 IEnumerator $$anonymous$$ove(GameObject Unit, Vector3 target) 
     {
         Vector3 startPosition = transform.position;
         while(Vector3.Distance(startPosition, target)>0.005f) 
         {
             transform.position=Vector3.$$anonymous$$oveTowards(startPosition, target, 7.0f*Time.deltaTime);
             yield return null;
         }
     }

This script is applied directly onto the unit

avatar image fafase · Mar 02, 2015 at 05:23 PM 0
Share

The first parameter should be the transform.position

avatar image samualsock · Mar 02, 2015 at 05:39 PM 0
Share

Alright I changed that and the movement is constant for the first right click but after that it gets gradually slower with each successive right click until it wont move at all

avatar image fafase · Mar 02, 2015 at 06:36 PM 1
Share

Could your issue be that you have multiple coroutines running at the same time and they just do it totally wrong, somehow canceling the effect of the previous ones?

While one is on, you should not be able to trigger a new one or you have to first cancel if any already running.

Also, avoid this:

  isSelectedScript isSelectedScript = GetComponent<isSelectedScript>();

your variable name is the same as the type. It seems to go but is not a good idea.

avatar image samualsock · Mar 02, 2015 at 08:48 PM 0
Share

Apparently my comment didn't post so I am going to post it again. I tried that and added in a stop coroutine but it still isnt working.

 using UnityEngine;
 using System.Collections;
 
 public class UnitControl : $$anonymous$$onoBehaviour {
     public GameObject[] Units;
     // Use this for initialization
     void Start () 
     {
 
 
 
 
     }
 
     bool unit$$anonymous$$oving = false;
 
     IEnumerator $$anonymous$$ove(GameObject Unit, Vector3 target) 
     {
         Vector3 startPosition = transform.position;
         while(Vector3.Distance(startPosition, target)>0.005f) 
         {
             transform.position=Vector3.$$anonymous$$oveTowards(transform.position, target, 7*Time.deltaTime);
             yield return null;
         }
     }
 
     // Update is called once per frame
     void FixedUpdate () 
     {
         float moveSpeed = 5.1f;
         if (Input.Get$$anonymous$$ouseButtonDown (1)) 
         {
             StopCoroutine("$$anonymous$$ove");
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit = new RaycastHit();
             if (Physics.Raycast (ray, out hit))
             {
                 Vector3 movePos = new Vector3 (hit.point.x, 1.5f, hit.point.z);
                 isSelectedScript selectedScript = GetComponent<isSelectedScript>();
                 Debug.Log("Debug working before isSelected loop");
 
                     if (selectedScript.isSelected == 1)
                     {
                         Debug.Log("This is between the if and the while loops");
                         transform.LookAt(hit.point);
                         StartCoroutine($$anonymous$$ove(gameObject, hit.point));
                     }
 
             }
         }
     }
 }
 
Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Examples of TBS/Heavily Gui-driven Games in Unity? 2 Answers

MMD How to export model and animations to Unity as 3rd person controller? 2 Answers

uUI - OnSelect: From Mouse/Pointer or Keyboard/Controller? 0 Answers

How can I make the script to play with the mouse? 0 Answers

Airplane Control, Aim ... Lerp and Vectors 0 Answers


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