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?
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...
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.
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.
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; }
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.
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;
}
}
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.
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;
}
}
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
Follow this Question
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