Hi, I am currently trying to write a script for unity. I have a puzzle game with blocks that need to be selected & moved one at a time. Currently I have the script almost working fine except when a block is selected, all the blocks move together instead of just the selected block. I am using the wasd keys to control the direction of the blocks. The script i’m using for movement is this…
public float speed;
private Rigidbody rb;
public void Start()
{
rb = GetComponent<Rigidbody>();
}
public void Update()
{
float h = Input.GetAxisRaw("Horizontal");
float v = Input.GetAxisRaw("Vertical");
Vector3 tempVect = new Vector3(h, 0, v);
tempVect = tempVect.normalized * speed * Time.deltaTime;
rb.MovePosition(transform.position + tempVect);
}
The script i’m using to select a block is this…
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseHoverColorChange : MonoBehaviour
{
//The object that this script is attached to.
private GameObject m_Object;
// The original color of the object.
private Color m_OriginalColor;
// The color that the object will change to when the mouse hovers over it.
public Color hoverColor = Color.red;
// The color of the object will change to red once selected.
public Color clickColor = Color.red;
private MoveObject moveObject;
// Start is called before the first frame update
void Start()
{
// Get the object that this script is attached to.
m_Object = gameObject;
// Get the original color of the object.
m_OriginalColor = m_Object.GetComponent<Renderer>().material.color;
// Get the block controller script from the object
moveObject = m_Object.GetComponent<MoveObject>();
}
private void OnMouseEnter()
{
// Change the color of the object to the hover color.
m_Object.GetComponent<Renderer>().material.color = hoverColor;
}
private void OnMouseExit()
{
// Change the color of the object back to it's original color.
m_Object.GetComponent<Renderer>().material.color = m_OriginalColor;
}
private void OnMouseDown()
{
m_Object.GetComponent<Renderer>().material.color = clickColor;
// toggle the block controller script
moveObject.enabled = !moveObject.enabled;
}
// Update is called once per frame
void Update()
{
}
}
Does anyone know how I can select just one block & move that instead of it moving all the blocks at the same time?