Reputation: 1360
I have 3 Game Objects
. These 3 Game Objects are my right , up and left buttons.
But I have no idea to control and check which of them clicked to action on player !
Note : I can do this by creating special C# for each of game objects by using OnMouseDown Method but I want do this in one Script file attached to player .
and this is one of my Game Objects
for Control Player
Upvotes: 0
Views: 950
Reputation: 125275
Note : I can do this by creating special C# for each of game objects by using OnmouseDown Method but i want do this in one Script file attached to player .
This shouldn't be a problem at-all. You can have two scripts communicate with each other.
Method 1:
Notify the PlayerController
script when there is a click action from other script.
Attach to each GameObject to detect click on. It sends the GameObject that was clicked to your PlayerController
script.
public class ClickDetector: MonoBehaviour
{
PlayerController playerController;
void Start()
{
playerController = GameObject.Find("GameObjectPlayerControlIsAttachedTo").GetComponent<PlayerController>();
}
void OnMouseDown()
{
//Notify our PlayerController script that there was a click
playerController.OnGameObjectClicked(this.gameObject);
}
}
Add this to your PlayerController
script. It receives the GameObject that was clicked. You can compare with the GameObject name
or tag
. I suggest you go with a tag
.
public class PlayerController: MonoBehaviour
{
public void OnGameObjectClicked(GameObject objClicked)
{
Debug.Log("There was a click from: "+objClicked.name);
if (objClicked.CompareTag("right"))
{
//Your code
}
else if (objClicked.CompareTag("left"))
{
//Your code
}
else if (objClicked.CompareTag("up"))
{
//Your code
}
}
}
Method 2:
Do everything in one script (PlayerController
).
You can use a raycast
. I suggest you use tag
to compare which gameobject is clicked. Create 3 tags(right
,left
and up
) from the Editor and put each GameObject to the right tag
. If you don't want to use tag, you can replace the if (rayCastHit.collider.CompareTag("up"))
with if (rayCastHit.collider.name=="up")
.
void Update()
{
//Check if Mouse Button is pressed
if (Input.GetMouseButtonDown(0))
{
//Raycast from mouse cursor pos
RaycastHit rayCastHit;
Ray rayCast = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(rayCast, out rayCastHit))
{
if (rayCastHit.collider.CompareTag("right"))
{
//Your code
}
else if (rayCastHit.collider.CompareTag("left"))
{
//Your code
}
else if (rayCastHit.collider.CompareTag("up"))
{
//Your code
}
}
}
}
EDIT:
After updating your question, you need to attach a collider to your GameObjects for raycast to work. Attach Box Collider 2D to each of the 3 GameObjects.
Also, since this is a 2D sprite, The code for Method 2 changes a little bit:
if (Input.GetMouseButtonDown(0))
{
Vector2 ray = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit2D rayHit = Physics2D.Raycast(ray, Vector2.zero);
if (rayHit)
{
if (rayHit.collider.CompareTag("up"))
{
}
}
}
Finally, from what you are doing, it looks like you need a Virtual JoyStick and you are current doing it wrong. The current solution should work for you but the right way to do this is with the UI system (Image
component).
Upvotes: 1