Search Unity

Help for tactical movement needed !

Discussion in 'Scripting' started by Sir_Mister_Lord_Emperor, Feb 17, 2018.

  1. Sir_Mister_Lord_Emperor

    Sir_Mister_Lord_Emperor

    Joined:
    Feb 17, 2018
    Posts:
    8
    Hello, I am new at C# and I need help to make a script that:

    - detects when I click within the 2D polygon collider of a sprite renderer that is a child of a character mesh (so it allays follows it).

    - then if I clicked in on the collider it moves my character where I clicked, but if I click outside the collider it doesn’t move.

    - given a parameter of “MovementPoints” the 2D polygon collider of a sprite renderer also grows/shrinks.

    - given a parameter of “Movementpoints” it detects how much are used based on the distance traveled and stores the information


    So far I got the click to move part but I can’t make it constraint only to the collider. My final goal is for that to be the movement system of a turn based tactical game. If there is a better way to do this I am open to suggestions. but for now here is my script:


    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. using UnityEngine.AI;
    7.  
    8. [RequireComponent(typeof(PlayerMotor))] // here I deal with the player agent for the nav mesh
    9.  
    10. [RequireComponent(typeof(PlayerStats))] // the movement points come from here
    11. public class TacticalMovement : MonoBehaviour {
    12.  
    13.  
    14.     public LayerMask movementMask;
    15.  
    16.     Camera cam;
    17.     PlayerMotor motor;
    18.     Ray ray;
    19.     SpriteRenderer walk;
    20.  
    21.     void Start () {
    22.         cam = Camera.main;
    23.         motor = GetComponent<PlayerMotor> ();
    24.     }
    25.  
    26.     void Update () {
    27.         if (Input.GetMouseButtonDown (0)) {
    28.  
    29.             Ray ray = cam.ScreenPointToRay (Input.mousePosition);
    30.             walk = GetComponent<SpriteRenderer>();
    31.  
    32.  
    33.             RaycastHit hit;
    34.  
    35.             if (Physics.Raycast (ray, out hit, 100, movementMask )) {
    36.  
    37.                 MeshCollider walk;
    38.  
    39.  
    40. if (walk = hit.transform.GetComponent<MeshCollider> ()) { // here I tried to add a second check for the mouse hit on the colider
    41.  
    42.  
    43.  
    44.  
    45.                     motor.MoveToPoint (hit.point);
    46.  
    47. }
    48.  
    49.                 }
    50.  
    51.  
    52.  
    53.             }
    54.         }
    55.  
    56.     }
    57.  
    58.  
    PS: I don’t know yet how exactly I will indicate the equivalent movement per 1 MovementPoint, perhaps via multiple transparent layers that stack on each other and there is a layer 1,2,3… etc. for each MomentPoint, but that seems like a crappy solution and the goals above are much more important to nail right.
     
    Last edited: Feb 18, 2018
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Use code tags. Check the first thread in this forum.
     
  3. AnsonRutherford

    AnsonRutherford

    Joined:
    Jan 16, 2014
    Posts:
    12
    First off, you should use the code tags to make your code easier to read. There's a stickied thread about how to do that.

    Secondly, you're on the right track with what you've written so far. You need to use "==" instead of "=" in your second if statement though. If you've got a bunch of different colliders you want to use, you'll need to check against all of them. You can do this using a "for" or "foreach" loop, and you can get the different colliders of your parent by using GetComponentsInChildren.

    I'm not sure I understand your other questions.