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:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.AI;
[RequireComponent(typeof(PlayerMotor))] // here I deal with the player agent for the nav mesh
[RequireComponent(typeof(PlayerStats))] // the movement points come from here
public class TacticalMovement : MonoBehaviour {
public LayerMask movementMask;
Camera cam;
PlayerMotor motor;
Ray ray;
SpriteRenderer walk;
void Start () {
cam = Camera.main;
motor = GetComponent<PlayerMotor> ();
}
void Update () {
if (Input.GetMouseButtonDown (0)) {
Ray ray = cam.ScreenPointToRay (Input.mousePosition);
walk = GetComponent<SpriteRenderer>();
RaycastHit hit;
if (Physics.Raycast (ray, out hit, 100, movementMask )) {
MeshCollider walk;
if (walk = hit.transform.GetComponent<MeshCollider> ()) { // here I tried to add a second check for the mouse hit on the colider
motor.MoveToPoint (hit.point);
}
}
}
}
}
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.