Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

RTS movement problems.

Discussion in 'Scripting' started by N1warhead, Jan 23, 2015.

  1. N1warhead

    N1warhead

    Joined:
    Mar 12, 2014
    Posts:
    3,884
    (please help me, I can never get Answers to pop up tags for me to select)

    Hey guys, here's my problem, I'm getting the character to move when I hit click.
    You know, I select the player, hit click and move.

    Well, problem is, I have the player to walk to the raycast hit.point.

    Well it just follows the mouse position everywhere.
    That's all it does.

    I've been fighting with this for a couple days now.

    here is my code. I'll only show the parts that need to be seen as it's a few hundred lines of code lol.


    Code (CSharp):
    1. void SingleUnitMovement(){
    2.        if (unitManager.UnitSelected) {
    3.                RaycastHit hit;
    4.                Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    5.                if (Map.collider.Raycast (ray, out hit, Mathf.Infinity)) {
    6.             if(Input.GetButtonDown("Fire1")){
    7.               MoveUnit = true;
    8.                //transform.position = hit.point;
    9.             }
    10.  
    11.          }
    12.          if(MoveUnit){
    13.          //   hit.point = Vector3.zero;
    14.            transform.LookAt(hit.point);
    15.            //unitManager.UnitVector = transform.Translate(Vector3.forward * Speed * Time.deltaTime);
    16.            //transform.LookAt(hit.point);
    17.            transform.Translate(Vector3.forward * Speed * Time.deltaTime);
    18.          //transform.position = Vector3.Lerp(transform.position, hit.point, Time.deltaTime * Speed);
    19.          //   hit.point = Vector3.zero;
    20.  
    21.          }
    22.        }
    23.  
    24.      }
    A lot of this is commented out, as I never know if I'll need it again.
    But, would be appreciated if I could get some insight on this.


    Something tells me it's something I am messing up with the bools, but if I cancel out the Raycast bool, then the player just stops moving after a frame or two.

    I thought about co-routines, but I don't want it to take the player seconds to walk a foot, I need a steady consistent speed no matter the distance the player needs to move.
     
  2. Thoras

    Thoras

    Joined:
    Oct 20, 2014
    Posts:
    3
    I'm not 100% sure how this part works (if (Map.collider.Raycast (ray, out hit, Mathf.Infinity))), but I believe that the variable hit is set in that "out hit" section, right?

    Is this then constantly updating your hit location, which would constantly change the location your unit is pointing towards when running "transform.LookAt(hit.point)" later?

    Have you tried to switch the locations of your raycast if with your buttodown if, so that it's only checked/updated when the button is pushed?
     
  3. N1warhead

    N1warhead

    Joined:
    Mar 12, 2014
    Posts:
    3,884
    That's the part that's complicated.

    How would I get the saved location of the click?
    Once I click it's constantly updating where the mouse position is.

    I can't figure out how to stop it, if I cut off any of the bools it just stops moving. It's weird lol.
     
  4. Thoras

    Thoras

    Joined:
    Oct 20, 2014
    Posts:
    3
    Have you tried not cutting the bools off, but switching their position?

    Code (CSharp):
    1. if (unitManager.UnitSelected)
    2. {
    3.    RaycastHit hit;
    4.    Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    5.  
    6.    if(Input.GetButtonDown("Fire1"))
    7.    {
    8.       if (Map.collider.Raycast (ray, out hit, Mathf.Infinity))
    9.       {
    10.          MoveUnit = true;
    11.       }
    12.    }
     
  5. N1warhead

    N1warhead

    Joined:
    Mar 12, 2014
    Posts:
    3,884
    When I do that, it says "Use of unassigned local variable 'hit'."
    I swear I freaking hate raycasts, they are the most hated thing I ever have to deal with, I'd rather program a wicked AI that takes months to make than deal with a stupid little raycast.

    I freaking despise them, but it's the only choice I have in this matter.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,501
    You have to break this process down a little bit further, into the separate states involved. Once the unit has been selected, the states might be something like this:

    - user moving cursor around screen to indicate destination
    - perhaps an indicator marker (cursor) is painted on the ground where the mouse is?
    - upon click, the selected unit begins to move:
    - - further input is disabled
    - - the unit proceeds through his movement either by:
    - - - a coroutine
    - - - using the navmesh with .setdestination
    - upon arrival at the destination:
    - - re-enable input

    Each of those states needs to essentially be accounted for and sequenced ("gated" if you will) appropriately in time and based on conditions and input.
     
  7. N1warhead

    N1warhead

    Joined:
    Mar 12, 2014
    Posts:
    3,884
    Hey, thanks buddy.

    I'll try this.

    I will try the NavMesh, because Co-Routines, I have to have a timer for it to get somewhere don't I?
    Like if I say it takes 5 seconds to get somewhere, if I move 1 foot, it would be crazy to use 5 seconds to move 1 foot.

    But I'll try some of this stuff that you mentioned.
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,501
    You don't need a timer for either coroutines or navmesh. A coroutine would simply be a way for you to "drive" your object to its destination over a given period of time, so in that sense you could call it a timer.

    As for the navmesh, you just set a destination on the agent, then wait for it to arrive, either by checking its distance to the destination, or else putting a temporary trigger around the target area and waiting for it to "bump."
     
  9. N1warhead

    N1warhead

    Joined:
    Mar 12, 2014
    Posts:
    3,884
    OMG THANK YOU, THE NAVMESH WORKS!