Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by EscapedGoat · Nov 14, 2015 at 10:07 PM · rotationuiphysicstextureobject

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();
 }
 
 



Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image EscapedGoat · Nov 15, 2015 at 05:49 AM 0
Share

@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

avatar image EscapedGoat EscapedGoat · Nov 15, 2015 at 01:58 PM 0
Share

@killer zog, You there bro?

avatar image killer-zog EscapedGoat · Nov 15, 2015 at 05:57 PM 1
Share

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.

Show more comments
avatar image EscapedGoat · Nov 16, 2015 at 04:36 PM 0
Share

@killer zog it works!!! THAN$$anonymous$$ YOU SO $$anonymous$$UCH FOR YOUR TI$$anonymous$$E!!!

avatar image Bwhang · Nov 02, 2016 at 07:42 AM 0
Share

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.

avatar image killer-zog Bwhang · Nov 02, 2016 at 07:56 AM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Texture help 0 Answers

Add force at position with respect to rotation 1 Answer

Rotating GUI Texture By Angle 2 Answers

UI TextMesh Pro disappears when rotated to exactly face the camera. 0 Answers

How not to make a RigidBody not go into ground when tilted foward? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges