- Home /
how to make player jump from clicking a box collider?
Hi I need help with making my player jump by clicking another object. So, I have a script on my player that says when I click anywhere on the screen it will jump. What I'm trying to do is make a split screen where there are two box colliders and one controls player 1 and the other controls player two. I don't know how to find the player game object if I click the box to make my player jump. May someone help me figure this out please?
I'm scripting in csharp by the way... Thanks ahead of time if anyone knows the answer.
If you run into trouble I can try to help you. I'am no expert but my first project was something like that, I know how sometimes little things are so frustrating :b
Answer by Berenger · Jan 05, 2015 at 02:24 PM
There is several ways to do such a thing :
Use GameObject.Find(player's name) => slow.
Use a different tag on each one and then use FindGameObjectWithTag.
Declare a public variable in the script that receive the click, then assign the player to it.
In the player script's update, check for Input.GetMouseButtonDown, the check the position of the mouse compared to to half the screen width.
Then jump.
Answer by Codessaurus · Jan 05, 2015 at 02:59 PM
When you are creating your player instances you should instanciate your prefab for the box controller with a script (for example: BoxControllerScript), where you should store your players name.
Then create a script for the player aswell, and inside the update function check for mouse clicks. When there is a mouse click use a raycast to get the object you are pointing at.
At this point you get the gameobject that you pointed at, check if it has the BoxControllerScript and if the player name match with the stored one, if it does, just jump.
Your answer
Follow this Question
Related Questions
Dash in movement direction, not in forward direction 0 Answers
How do I get my jump animation to work? 1 Answer
Jump Function C# hassle! 0 Answers
time counter in scripting 2 Answers
Jump Further When Key Is Held Down 1 Answer