- Home /
How to use On-Screen Buttons in unity 5 {URGENT}
Hello dear Forum, I have a question about the game I am currently developing. Its a ball game and the aim is that the ball moves to the end, my only problem is that I cant seem to find a suiting way to use On screen touch buttons, so that I can use my App on a mobile device, because at the moment I can only use "WASD" So if you could find a way to fix my Script (Attached Below), so that I can add buttons to move my ball for mobile that would be really helpful. This is the Script that is linked to my Ball object. Any reply would be great! P.S. I'm not the best at coding Thanks in the future.
IT WOULD MEAN A LOT IF YOU COULD HELP ME...
#pragma strict
var rotationSpeed = 100;
var jumpHeight = 0.1;
var Hit01 : AudioClip;
var Hit02 : AudioClip;
var Hit03 : AudioClip;
var distToGround : float;
function Start() {
distToGround = GetComponent.<Collider>().bounds.extents.y;
}
function Update ()
{
//Handel ball roation (right; left)
var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
rotation *= Time.deltaTime;
GetComponent.<Rigidbody>().AddRelativeTorque (Vector3.back * rotation);
if (Input.GetKeyDown(KeyCode.UpArrow) && IsGrounded ())
{
GetComponent.<Rigidbody>().velocity.y = jumpHeight;
}
}
function IsGrounded () : boolean {
return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1);
}
function OnCollisionEnter () {
var theHit = Random.Range(0, 3);
if (theHit == 0)
{
GetComponent.<AudioSource>().clip = Hit01;
}
else if (theHit == 1)
{
GetComponent.<AudioSource>().clip = Hit02;
}
else {
GetComponent.<AudioSource>().clip = Hit03;
}
GetComponent.<AudioSource>().pitch = Random.Range (0.9,1.1);
GetComponent.<AudioSource>().Play();
}
Answer by killer-zog · Nov 14, 2015 at 11:41 PM
HERE IS HOW TO DO IT IN DETAIL:
first of, create a player gameobject, and attach the script to it: the script is posted below
then create a button UI object and go to it's OnClick tab, and assign player
gameobject as the target.
then click on the dropdown menu there and locate the jumpPlayer
method. this means whenever the button is clicked the following method of the following target will execute!
now if everything is right, it should work in mobile too.
clicking the button with a mouse or a touch pressing should execute the method and make the player jump.
HERE IS THE FULL SCRIPT:
#pragma strict
#pragma strict
@Header ("Player Settings")
var rotationSpeed = 100;
var jumpHeight = 0.1;
var distToGround : float;
@Header ("Player Hit Audio")
var Hit01 : AudioClip;
var Hit02 : AudioClip;
var Hit03 : AudioClip;
@Header ("Movement Setting")
var forwardVelocity : float = 5;
var backwardVelocity : float = -5;
@HideInInspector
var player_rigidbody : Rigidbody ;
function Start() {
distToGround = GetComponent.<Collider>().bounds.extents.y;
player_rigidbody = GetComponent.<Rigidbody>(); // Get this objects rigidbody
}
function Update ()
{
//Handel ball roation (right; left)
var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
rotation *= Time.deltaTime;
GetComponent.<Rigidbody>().AddRelativeTorque (Vector3.back * rotation);
if (Input.GetKeyDown(KeyCode.UpArrow) && IsGrounded ())
{
playerJump();
}
if (Input.GetKey(KeyCode.LeftArrow))
{
playerMove(-1);
}
else if(Input.GetKey(KeyCode.RightArrow))
{
playerMove(1);
}
}
function playerJump(){
if (IsGrounded ())
{
GetComponent.<Rigidbody>().velocity.y = jumpHeight;
}
}
function playerMove(Dir : int){
var rVel : Vector3 = player_rigidbody.velocity;
rVel.x = (Dir == 1) ? forwardVelocity : backwardVelocity;
player_rigidbody.velocity = rVel;
}
function IsGrounded () : boolean {
return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1);
}
function OnCollisionEnter () {
var theHit = Random.Range(0, 3);
if (theHit == 0)
{
GetComponent.<AudioSource>().clip = Hit01;
}
else if (theHit == 1)
{
GetComponent.<AudioSource>().clip = Hit02;
}
else {
GetComponent.<AudioSource>().clip = Hit03;
}
GetComponent.<AudioSource>().pitch = Random.Range (0.9,1.1);
GetComponent.<AudioSource>().Play();
}
I HAVE UPDATED THE SCRIPT INCLUDING THE MOVEMENT FUNCTION! the method is same when assigning to the move right and left UI button, in the dropdownmenu look for playerMove(int Dir)
and assign it with the OnCLick.
ALSO YOU NEED TO ASSIGN A INTEGER VALUE. USE 1 OR -1 DEPENDING ON YOUR DIRECTION.
@killer zog Thank you so much for your reply, and your time. I have tried to do that with the OnClick function, but it never seemed to work. It makes sense what you said, but my only question is, the script that you made, doesn't that make the player only moveable with the keyboard buttons?
And if you could please just send me a full script of what I have to use, I would appreciate that soo much, because I will mess up in the script somewhere, and like I said, I'm not the best coder.
But once again, I understand where you are going with the OnClick, and I know how to implement it, just the scripting part is difficult for me.
Thanks for your time, and hope to hear from you soon
do you have any existing script that can move the ball?? if so post it so i can modify it, or i do have a script that can do it efficiently.
@killer zog it works!!! THAN$$anonymous$$ YOU SO $$anonymous$$UCH FOR YOUR TI$$anonymous$$E!!!
I've been working on this for some time before co$$anonymous$$g across this answer. It's been very helpful.
Two problems though. I noticed the ball movement method for the onscreen buttons uses a different method and when I use the code above, the ball moves when I release the button and not when the button is held.
I'm using 5.4.1.
Is there a particular reason why a new method for movement (starting on line 58) was created rather than the existing movement code on line 31? I'd like my Android ball to move just like the pc ball so trying to convert my movement code into a function that's broken into Left and Right isn't easy.
I was hoping Unity had a method where you could equate the onscreen button press to an input: Left and Right. But I can't find anything like this.
This would make it a lot easier and make the onscreen buttons use the same movement code as the pc version.
hi @Bwhang , this should work in latest version. But i am not sure what is the issue. Also this post is a year old. I no longer use unity, i switched to UE4. sorry :(
try to create a new question regarding this, someone will answer..... hopefully.