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 · Feb 08, 2015 at 01:48 PM · movementstrategymouse inputreal timemouse control

RTS movement not working properly

I am fairly new to unity although I have some prior experience coding in C#. I am trying to make a basic RTS game and have been working on getting units to move properly and was looking for some help.

On my unit I have the following script`using UnityEngine; using System.Collections;

public class UnitSelect : MonoBehaviour {

 // Use this for initialization
 void Start () 
 {

 }

 public int isHit;
 public int isSelected;
 // Update is called once per frame
 void Update () 
 {

     if (Input.GetMouseButtonDown(0)) 
     {
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit = new RaycastHit();

             if(Physics.Raycast(ray,out hit))
         {
             isHit = 0;
             isSelected = 1;
             //Destroy(GameObject.Find(hit.collider.gameObject.name));

         }
     }
     
 }

} ` and then on my camera I have this script using UnityEngine; using System.Collections;

 public class Movement : MonoBehaviour 
 {
     public GameObject unit;
     public GameObject unit1;
     public float unitSpeed = 0.00001f;
 
     private UnitSelect unitSelect;
 
     void Awake()
     {
         unitSelect = unit.GetComponent<UnitSelect> ();
         unit1 = GameObject.Find("SphereUnit");
     }
     // Use this for initialization
     void Start () 
     {
                 
     }
     
     // Update is called once per frame
     void Update ()         
     {
         if (Input.GetMouseButtonDown (1)) 
         {
             Ray ray2 = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit2 = new RaycastHit();
             if (Physics.Raycast(ray2, out hit2));
             {
 
 
                 Debug.Log("Right click works");
 
                 if (unitSelect.isSelected == 1)
                 {
                     Debug.Log("Working");
                     unit1.renderer.material.color = Color.red;
                     unit1.transform.Translate( hit2.point * unitSpeed * Time.deltaTime);
                 }
             }
         }
     }
 }

The problem I am having is that the unit will move only a little way to the destination of the mouse click and then if you click somewhere else it will move in a seemingly random direction. I am also unsure on how to get this to work for multiple units so any suggestions for that would be welcome. I have Googled this issue but all the answers I found where working in Jacascript.

Thanks :)

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by daneislazy · Feb 08, 2015 at 02:46 PM

So this is basically a case of Translate not actually doing what you think it will do.

Firstly, it just changes the transform position like doing transform.position += destination and since your multiplying by Time.deltaTime the magnitude of the vector is shrunk down a lot so it only moves a little bit.

Secondly, since you are not getting the vector of the difference between the current point and the target point it's not a correct vector so that's possibly what's causing the second issue.

Another cause could be if the transform is rotating when moving. If rotated, doing the translate may cause the movement to be relative to the wrong direction and thus be just wrong.

As for solving all of these problems and using it with multiple units, check out the Unity tutorial on coroutines. http://unity3d.com/learn/tutorials/modules/intermediate/scripting/coroutines

It's also worth noting the tutorial is kind of using Lerp wrong so you will probably want to change that or use MoveTowards instead. And add some logic for controlling the speed of the unit.

Hope that helps!

Comment
Add comment · Show 5 · 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 · Feb 08, 2015 at 03:05 PM 0
Share

Thank you for your answer! I have been trying to work out why it wasn't working for a while before I came to ask here. So would you recommend using a coroutine for all unit movement then?

avatar image daneislazy · Feb 08, 2015 at 03:41 PM 0
Share

possibly, it's mostly just personal preference; if you are running it with yield return null; it works out to be the same as running a normal method/function every Update(). but you can run it with a waitforseconds ins$$anonymous$$d or just run a method every FixedUpdate() since that's when all the physics stuff is calculated anyways so it should be fine, unless a unit is really fast.

For multiple units you could probably use an Array/List/Dictionary of all the GameObjects and their destination points and just iterate through it ins$$anonymous$$d of having a ton of methods/coroutines running(`Dictionary<GameObject, Vector3>` probably best).

I haven't really had to use something like this though; so whatever works.

edit: it keeps $$anonymous$$ring out my less than & greater than symbols

avatar image samualsock · Feb 08, 2015 at 04:31 PM 0
Share

Yeah that's similar to what I was planning. An array of all the units in the map and a check to see if they are selected.

So what would the correct code be to move the selected unit to the mouse click? I am using ray casting but am unsure of exactly how to implement it and tutorials aren't helping

EDIT: From what I understand, I basically want to be doing something

if (mouse click) { move unit to mouse click }

avatar image daneislazy · Feb 08, 2015 at 07:22 PM 0
Share

It's basically covered in that video that I linked, there are also copies of the scripts he used under the video, the $$anonymous$$ouse scripts and the second coroutine script are basically bang on.

raycasting seems fine, if the ray is hitting multiple things, you could iterate through 'till you get ground or some tag you want. But yea If(mouseclick && unitSelected){ set target position for unit; } if you put the coroutine on every unit you could have a public targetVector3 in all the unit scripts and you could just set it with a master click script. gameObjectSelected.targetVector3 = clickPosition; again basically what they did in the tutorial. $$anonymous$$ain difference will be the public in the mouse script will be a big array of units. And use Vector3.$$anonymous$$oveTowards ins$$anonymous$$d of Lerp.

avatar image samualsock · Feb 08, 2015 at 08:49 PM 0
Share

Alright, thank you for all your help! :D

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

Mouse Flight Doesn't Allow for Rolling 1 Answer

Character movement range in strategy game. 2 Answers

Move object to mouse position on pressing fire2? 1 Answer

Trying to keep an object 1.5 above the terrain at all times 1 Answer

Detecting Object On Tile 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