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 /
  • Help Room /
avatar image
0
Question by yepez7 · May 31, 2016 at 01:11 PM · buttonmobilescreen

how can I specify an area based on a proportion of the screen height/width and have a jump/move image there?

how can I specify an area based on a proportion of the screen height/width for a mobile device and have a jump/move image there?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Mmmpies · May 31, 2016 at 01:48 PM

Easy way is use the UI. Add a canvas to your game then add a button to the canvas. Scale the button to be the size you want then put the anchors at the corner of the button. That will make the button scale with differing screen sizes so it should stay the right size relative to screen size. Like this...

alt text

Then we modify that script we talked about in the last question...

 using System.Collections;
 
 public class ButtonMovement : MonoBehaviour {
 
     public Rigidbody2D playerRigidbody;
     private float jumpHeight = 2f;
     private bool isGrounded;
     private float jumpHeldFor = 0f;
     private bool jumpHeld = false;
 
     // Update is called once per frame
     void Update()
     {
         if (jumpHeld)
             jumpHeldFor = jumpHeldFor + Time.deltaTime;
     }
 
     public void JumpPressed () {
         jumpHeld = true;
         }
 
     public void JumpReleased() {
         Debug.Log (jumpHeldFor);
         if (jumpHeldFor > 0f && isGrounded) {
             if (jumpHeldFor < 1.0f) {
                 playerRigidbody.AddForce (Vector2.up * jumpHeight, ForceMode2D.Impulse);
             } else if (jumpHeldFor >= 1.0f) {
                 playerRigidbody.AddForce (Vector2.up * jumpHeight * 6, ForceMode2D.Impulse);
             }
             jumpHeldFor = 0f;
             jumpHeld = false;
         }
     }
 
     void OnCollisionEnter2D(Collision2D col)
     {
         if (col.gameObject.name == "ground")
             isGrounded = true;
     }
 
     void OnCollisionExit2D(Collision2D col)
     {
         if (col.gameObject.name == "ground")
             isGrounded = false;
     }
 }

Now go to the button in the inspector and add an Event Trigger component. Add two event types (Pointer Down and Pointer Up). In both drag the player (that should have that script attached to it) onto the empty slot.

Then select the function and choose ButtonMovement -> JumpPressed for Pointer Down and ButtonMovement -> JumpReleased for Pointer Up.

alt text

And that's it, the UI will handle mouse or touch in the same way. You don't need an image for the button so it just looks transparent on the end device.


anchorsspng.png (7.4 kB)
eventspng.png (41.1 kB)
Comment
Add comment · Show 5 · 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 yepez7 · Jun 01, 2016 at 01:29 PM 0
Share

I did it exactly as u told me but my player don't move i did some debug and it does call JumpPressed and JumpReleased but don't jump.

public enum type {LeftButton, RightButton, JumpButton,PouseButton}; public type buttonType;

public float jumpHeight = 0.0f, moveSpeed = 0.0f; public float lastClick = 0f; public int isOn = 0; public float myTimeDeltaTime = 0f;

public GameObject playerObject = null; Rigidbody2D playerRigidbody;

public GUITexture buttonTexture = null;

private float myTouchDT = 0; private Touch touch; private bool isGrounded;

private float jumpHeldFor = 0f; private bool jumpHeld = false;

// Use this for initialization void Start () { playerRigidbody = playerObject.GetComponent();

} void Update () {

 if (Input.touchCount > 0) {
     touch = Input.GetTouch (0);
     myTouchDT = myTouchDT + Time.deltaTime;
 }

 if (jumpHeld) {
     jumpHeldFor = jumpHeldFor + Time.deltaTime;
 }

}

public void JumpPressed () { jumpHeld = true; Debug.Log ("jumpPress is true");

}

public void JumpReleased() {

 Debug.Log ("jumpReased is true");

 Debug.Log (jumpHeldFor);
 if (jumpHeldFor > 0f && isGrounded) {

     if (jumpHeldFor < 1.0f) {
         Debug.Log ("small jump");
         playerRigidbody.AddForce (Vector2.up * jumpHeight, Force$$anonymous$$ode2D.Impulse);
     } else if (jumpHeldFor >= 1.0f) {
         playerRigidbody.AddForce (Vector2.up * jumpHeight * 6, Force$$anonymous$$ode2D.Impulse);
         Debug.Log ("big jump");

     }
     jumpHeldFor = 0f;
     jumpHeld = false;
 }

}

void OnCollisionEnter2D(Collision2D col) { if (col.gameObject.name == "ground") isGrounded = true; }

void OnCollisionExit2D(Collision2D col) { if (col.gameObject.name == "ground") isGrounded = false; }

avatar image Mmmpies yepez7 · Jun 01, 2016 at 01:36 PM 0
Share

First did you drag the player onto the Rigidbody slot?

Next did you change the jumpheight to something other than 0.0f?

By instinct I'd suggest the jumpheight because I'd have thought you'd get an error otherwise.

avatar image yepez7 Mmmpies · Jun 01, 2016 at 01:41 PM 0
Share

yes i did both.

avatar image yepez7 · Jun 01, 2016 at 02:09 PM 0
Share

i did it like this if the ground had to do something and it did something is wrong with grounded. so the thing that is wrong is grounded

public void JumpReleased() {

         Debug.Log ("jumpReased is true");

         Debug.Log (jumpHeldFor);
         if (jumpHeldFor > 0f /*&& isGrounded*/)
     {
         Debug.Log ("jumpReased is grounded");

             if (jumpHeldFor < 1.0f) 
         {
                 Debug.Log ("small jump");
                 playerRigidbody.AddForce (Vector2.up * jumpHeight, Force$$anonymous$$ode2D.Impulse);
         } else if (jumpHeldFor >= 1.0f) 
             { // aqui ira isIt$$anonymous$$oving pa que lo active
                 playerRigidbody.AddForce (Vector2.up * jumpHeight * 6, Force$$anonymous$$ode2D.Impulse);
                 Debug.Log ("big jump");

             }
             jumpHeldFor = 0f;
             jumpHeld = false;
     }

 }
avatar image Mmmpies yepez7 · Jun 01, 2016 at 02:19 PM 0
Share

This is your problem...

 public GameObject playerObject = null; Rigidbody2D playerRigidbody;

The public GameObject line followed immediately by the rigibdopby line but tha rigidbody isn't public so you haven't copied the Rigidboy in the inspector you've copied the gameObject ins$$anonymous$$d.

This works Note you no longer need the touch stuff in Update and you DO need using UnityEngine.UI;

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class B$$anonymous$$ : $$anonymous$$onoBehaviour {
 
     public enum type {LeftButton, RightButton, JumpButton,PouseButton}; public type buttonType;
 
     public float jumpHeight = 0.0f, moveSpeed = 0.0f; 
     public float lastClick = 0f; 
     public int isOn = 0; 
     public float myTimeDeltaTime = 0f;
 
     //public GameObject playerObject = null; 
     public Rigidbody2D playerRigidbody;
 
     public GUITexture buttonTexture = null;
 
     private float myTouchDT = 0; 
     private Touch touch; 
     private bool isGrounded;
 
     private float jumpHeldFor = 0f; 
     private bool jumpHeld = false;
 
     // Use this for initialization void Start () { playerRigidbody = playerObject.GetComponent();
 
     void Update () {
 
         if (jumpHeld) {
             jumpHeldFor = jumpHeldFor + Time.deltaTime;
         }
 
     }
 
     public void JumpPressed () {
         jumpHeld = true; Debug.Log ("jumpPress is true");
     }
 
     public void JumpReleased() {
 
         Debug.Log ("jumpReased is true");
 
         Debug.Log (jumpHeldFor);
         if (jumpHeldFor > 0f && isGrounded) {
 
             if (jumpHeldFor < 1.0f) {
                 Debug.Log ("small jump");
                 playerRigidbody.AddForce (Vector2.up * jumpHeight, Force$$anonymous$$ode2D.Impulse);
             } else if (jumpHeldFor >= 1.0f) {
                 playerRigidbody.AddForce (Vector2.up * jumpHeight * 6, Force$$anonymous$$ode2D.Impulse);
                 Debug.Log ("big jump");
 
             }
             jumpHeldFor = 0f;
             jumpHeld = false;
         }
 
     }
 
     void OnCollisionEnter2D(Collision2D col) { 
         if (col.gameObject.name == "ground") isGrounded = true; 
     }
 
     void OnCollisionExit2D(Collision2D col) { 
         if (col.gameObject.name == "ground") isGrounded = false; 
     }
 }

Also you keep postinng in the Answer section, it's confusing because the link says Reply (it doesn't mena that it means Answer) you should use the little reply under my post rather than the big one or add Comment.

avatar image
0

Answer by yepez7 · Jun 02, 2016 at 02:35 AM

no it didn't work this is my hold script

using UnityEngine; using System.Collections;

public class BottonMovement : TouchManeger { public enum type {LeftButton, RightButton, JumpButton,PouseButton}; public type buttonType;

 public float jumpHeight = 0.0f, moveSpeed  = 0.0f;
 public float  lastClick = 0f;
 public int isOn = 0;

 public  Rigidbody2D playerRigidbody;

 public GameObject playerObject;
 public GUITexture buttonTexture = null;
 public bool isItMoving = false;// if player moves
 private float myTouchDT = 0;
 private Touch touch;
 private bool isGrounded;

 private float jumpHeldFor = 0f;
 private bool jumpHeld = false;

 void Start () {
 }
 void Update () {
     if (jumpHeld) {
         jumpHeldFor = jumpHeldFor + Time.deltaTime;
     }
 }

 public void JumpPressed () {
     jumpHeld = true;
     Debug.Log ("jumpPress is true");

 }
 void OnCollisionEnter2D(Collision2D col)
 {
     if (col.gameObject.name == "ground")
         isGrounded = true;
     Debug.Log ("player == ground");

 }

 void OnCollisionExit2D(Collision2D col)
 {
     if (col.gameObject.name == "ground")
         isGrounded = false;
     Debug.Log ("player not ground");

 }

 public void JumpReleased()
 {

     Debug.Log ("jumpReased is true");
     Debug.Log (jumpHeldFor);
     if (jumpHeldFor > 0f && isGrounded){
         Debug.Log ("jumpReased is grounded");
         if (jumpHeldFor < 1.0f)
         {
             Debug.Log ("small jump");
             playerRigidbody.AddForce (Vector2.up * jumpHeight, ForceMode2D.Impulse);
         } else if (jumpHeldFor >= 1.0f)
         { // aqui ira isItMoving pa que lo active
             if (isItMoving)// if it moving you can use the big jum otherwice u can not
             {
                 playerRigidbody.AddForce (Vector2.up * jumpHeight * 1.5f, ForceMode2D.Impulse);
                 Debug.Log ("big jump");
             }
         }
         jumpHeldFor = 0f;
         jumpHeld = false;
     }

 }




 void FixedUpdate () {
     TouchInput (buttonTexture);
 }

 void OnFirstTouch()
 {
     switch (buttonType) {
     case type.LeftButton:
         isItMoving = true;// this is to activate holdjump
         playerObject.transform.eulerAngles = new Vector2 (0, 180);
         playerObject.transform.Translate (Vector2.right * moveSpeed * Time.deltaTime);//move left
         isOn = 1;
         break;
     case type.RightButton:
         isItMoving = true;

         playerObject.transform.eulerAngles = new Vector2 (0,0);
         playerObject.transform.Translate (Vector2.right * moveSpeed * Time.deltaTime);//move right
         isOn = 1;
         break;
     }
 }
 void OnSecondTouch()
 {
     switch (buttonType) {
     case type.LeftButton:
         isItMoving = true;

         playerObject.transform.eulerAngles = new Vector2 (0, 180);// mueve el mono 180 dgrees pa que camine left
         //playerObject.transform.Translate (-Vector2.right * moveSpeed * Time.deltaTime);//move left
         playerObject.transform.Translate (Vector2.right * moveSpeed * Time.deltaTime);//move left
         isOn = 1;
         break;
     case type.RightButton:
         isItMoving = true;

         playerObject.transform.eulerAngles = new Vector2 (0, 0);
         playerObject.transform.Translate (Vector2.right * moveSpeed * Time.deltaTime);//move right
         isOn = 1;
         break;
     }

 }

 void OnFirstTouchEnded()
 {
     isOn = 0;
 }
 void OnSecondTouchEnded()
 {
     isOn = 0;
 }

}

Comment
Add comment · Show 1 · 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 Mmmpies · Jun 02, 2016 at 07:17 AM 0
Share

Right I'm leaving this as an Answer so you can see that you've ANSWERED your question with a question. DONT click the big Reply link as that's for Answers. Click the Comment link.

Now you're mixing Touch input and UI input there really is no need UI input deals with Touch and $$anonymous$$ouse input so you can test in Unity before porting to your phone/tablet. It's also way easier and needs less program$$anonymous$$g skill.

Remove touch Input and just put a left and right UI button. You still need

 using UnityEngine.UI;


Then just have a PointerDown/PointerUp event for each then set a function to switch a bool to true or false e.g.

 public void LeftPointerDown () 
     {
         moveLeft = true;
     }
 
     public void RightPointerDown () 
     {
         moveRight = true;
     }
 
     public void LeftPointerUp ()
     {
         moveLeft = false;
     }
 
     public void RightPointerUp()
     {
         moveRight = false;
     }

And just use an if in in Update to handle movement

 if(moveLeft && moveRight)
 {
      //movement cancelled as both are pressed set is$$anonymous$$oving to false
 } else if (moveLeft)
 {
      /// do left movement here
 } else if (moveRight)
 {
      // do right movement here
 } else
 {
     //is moving to false
 }

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

66 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Mobile 2D touch sensitive areas on screen 0 Answers

How to make an object keep move when pressing a button? 0 Answers

Unity UI - Button Slow On Some Devices? 0 Answers

Buttons are shrinking in size on a mobile screen 0 Answers

UI Button Double jump 0 Answers


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