- Home /
How do I tie my movement script to only selected objects?,How do I tie my movement script to the my selection script?
I'm trying to make the player only move the selected item and not all at once. Right now I have a movement script that looks like this:
void Update()
{
if (Input.GetKey(KeyCode.A))
transform.Rotate(-Vector3.forward * RotateSpeed * Time.deltaTime);
else if (Input.GetKey(KeyCode.D))
transform.Rotate(Vector3.forward * RotateSpeed * Time.deltaTime);
}
And a selection script that looks like this:
void MouseInput()
{
// When Mouse 0 is pressed
if (Input.GetMouseButton(0))
{
// Raycast from camera to mouse position
Vector2 raycastPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(raycastPos, Vector2.zero);
// If a collider is hit
if (hit.collider != null)
{
// Log the name of the hit object
Debug.Log(hit.collider.gameObject.name);
}
if (hit.collider.gameObject.name != null)
{
Debug.Log("Im called " + hit.collider.gameObject.name + " and I'm hit and should move");
}
}
Unity does know when something is selected or "hit" and I have my movement script on all the game objects that need movement right now.
Just to clarify, your desired behaviour is to:
-Click (and hold) on an object
-Have it then be "selected"
-Once selected your A and D key inputs will move it
Let me know if that's correct.
Yes, with the click a want to select them (ideally with a button a press that cycles through them all) and then move it with A and D key.
Answer by HellsHand · Apr 13, 2021 at 04:06 PM
Here try this in 1 script on the objects you want the player to move:
using UnityEngine;
using UnityEngine.EventSystems;
public class RotateObject : MonoBehaviour, ISelectHandler, IDeselectHandler
{
bool selected = false;
void Update()
{
if (selected)
{
if (Input.GetKey(KeyCode.A))
transform.Rotate(-Vector3.forward * RotateSpeed* Time.deltaTime);
else if (Input.GetKey(KeyCode.D))
transform.Rotate(Vector3.forward * RotateSpeed* Time.deltaTime);
}
}
private void OnMouseDown()
{
if (Input.GetMouseButtonDown(0))
{
EventSystem.current.SetSelectedGameObject(gameObject);
}
}
public void OnSelect(BaseEventData eventData)
{
selected = true;
}
public void OnDeselect(BaseEventData eventData)
{
selected = false;
}
}
Unless I'm misunderstanding your problem that should work.
It looks like it would be solve my issue but I'm having a bit of a problem with using it because I am getting this error: Object reference not set to an instance of an object selected.Update () (at Assets/selected.cs:14) And I'm not sure how to fix it. My selected script looks like this:
void Update()
{
{
if (EventSystem.current.currentSelectedGameObject)
{
if (Input.GetKey(KeyCode.A))
transform.Rotate(-Vector3.forward * RotateSpeed * Time.deltaTime);
else if (Input.GetKey(KeyCode.D))
transform.Rotate(Vector3.forward * RotateSpeed * Time.deltaTime);
}
}
}
private void OnMouseDown()
{
if (Input.GetMouseButtonDown(0))
{
EventSystem.current.SetSelectedGameObject(gameObject);
}
}
}
Edit: So I think I need to let unity know that the item is also selected in the event system but I'm having a syntax error right now trying to do that and I don't know how to fix that.
// If a collider is hit
if (hit.collider != null)
{
// Log the name of the hit object
Debug.Log(hit.collider.gameObject.name);
EventSystem.SetSelectedGameObject(1Hole);
}
With the syntax error being: Assets\selection.cs(31,52): error CS1003: Syntax error, ',' expected