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 Sklyphrom · Sep 30, 2020 at 02:08 PM · movementcolorchangeturn-basedarea

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: alt text This is what I've achieved so far: alt text 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).

placeholderareaattemptcapt01.png (96.2 kB)
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
0
Best Answer

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);
         }
     }
 }
Comment
Add comment · 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

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

200 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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