- Home /
Turn-based-RPG-styled movement area color change
As the question implies, I'm trying to make Unity (2020.1.6f1) recreate something like this: This is what I've achieved so far: The catch is, I've not been able to make every plane's mesh collider inside the player's movement area turn blue at the same time; they only do so when the mouse passes over them. The code right now:
using UnityEngine;
public class GrassBehaviour : MonoBehaviour
{
public GameObject player;
public Vector3 playerOriginalPosition;
public Vector3 playerMaxPosition;
public Vector3 grassSelectedPosition;
public MeshRenderer[] rendArray;
public bool comparePos;
public bool mouseOver;
public bool mouseExit;
public Vector3 comparePos_2;
public Vector3 comparePos_3;
public void Start()
{
player = GameObject.FindGameObjectWithTag("Player");
}
public void FixedUpdate()
{
playerOriginalPosition = player.transform.position;
playerMaxPosition = new Vector3(playerOriginalPosition.x + 2f, 0.5f, playerOriginalPosition.z + 2f);
grassSelectedPosition = new Vector3(gameObject.transform.position.x, 0.5f, gameObject.transform.position.z);
rendArray = gameObject.GetComponents<MeshRenderer>();
comparePos = Vector3.Distance(playerMaxPosition, playerMaxPosition - (grassSelectedPosition - playerOriginalPosition)) < Vector3.Distance(playerMaxPosition, playerOriginalPosition);
comparePos_2 = new Vector3((playerMaxPosition - playerOriginalPosition).x, 0.5f, (playerMaxPosition - playerOriginalPosition).z);
comparePos_3 = new Vector3((float)(playerMaxPosition.x - playerMaxPosition.x - (grassSelectedPosition.x - playerOriginalPosition.x)), 0.5f, (playerMaxPosition.z - playerMaxPosition.z - (grassSelectedPosition.z - playerOriginalPosition.z)));
if (comparePos==true)
{
rendArray[rendArray.Length * (int)playerMaxPosition.magnitude].material.color = Color.red;
}
}
public void OnMouseOver()
{
if (comparePos == true)
{
mouseOver = true;
gameObject.GetComponent<MeshRenderer>().material.color = Color.blue;
}
else
{
mouseOver = false;
return;
}
}
public void OnMouseExit()
{
if(comparePos == false)
{
mouseExit = true;
gameObject.GetComponent<MeshRenderer>().material.color = Color.green;
}
else
{
mouseExit = false;
return;
}
}
public void OnMouseDown()
{
if (comparePos == true)
{
player.transform.position = new Vector3(gameObject.transform.position.x, player.transform.position.y, gameObject.transform.position.z);
}
else
{
return;
}
}
}
As you'll no doubt notice, I'm not much into vectors yet -.- I humbly await an answer (or a correction, I'm afraid).
Answer by Sklyphrom · Sep 30, 2020 at 08:18 PM
Solved the riddle myself! The new code:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GrassBehaviour : MonoBehaviour
{
public GameObject player;
public Vector3 playerOriginalPosition;
public Vector3 playerMaxPosition;
public Vector3 grassSelectedPosition;
public bool comparePos;
public bool mouseOver;
public bool mouseExit;
public void Start()
{
player = GameObject.FindGameObjectWithTag("Player");
}
public void FixedUpdate()
{
playerOriginalPosition = player.transform.position;
playerMaxPosition = new Vector3(playerOriginalPosition.x + 2f, 0.5f, playerOriginalPosition.z + 2f);
grassSelectedPosition = new Vector3(gameObject.transform.position.x, 0.5f, gameObject.transform.position.z);
comparePos = Vector3.Distance(playerMaxPosition, playerMaxPosition - (grassSelectedPosition - playerOriginalPosition)) < Vector3.Distance(playerMaxPosition, playerOriginalPosition);
if(comparePos == true)
{
GetComponent<MeshRenderer>().material.color = Color.cyan;
}
else if (comparePos != true)
{
GetComponent<MeshRenderer>().material.color = Color.green;
}
}
public void OnMouseOver()
{
if (comparePos == true)
{
gameObject.GetComponent<MeshRenderer>().material.color = Color.blue;
}
}
public void OnMouseDown()
{
if (comparePos == true)
{
player.transform.position = new Vector3(gameObject.transform.position.x, player.transform.position.y, gameObject.transform.position.z);
}
}
}
Your answer
Follow this Question
Related Questions
3D Grid Based Movement (X-Com 2012 etc) – How to implement vertical movement? 0 Answers
How can I get the pre-defined color of a NavMesh area ? 2 Answers
gui.label color change 1 Answer
Duplicated objects, different colors 1 Answer
How would I change the colour of a prefab relative to the size of the character? 1 Answer