- Home /
Get Touch touch phase began is grounded not working
i have no idea why my script is not working , it perfectly works with the up button , but with gui texture it doesn't.
using UnityEngine; using System.Collections;
public class jumpdugmemovement: MonoBehaviour {
private bool grounded = true;
public int jumpspeed = 350;
// Use this for initialization
void Start () {
}
void OnCollisionEnter(Collision other)
{
grounded = true; // collision with any object is grounded
}
void OnCollisionExit(Collision other)
{
grounded = false; // collision exit with any object is not grounded
}
// Update is called once per frame
void Update () {
if(Input.touches.Length <=0)
{
//some code
}
else
{
for(int i=0;i<Input.touchCount;i++)
{
if(this.guiTexture.HitTest(Input.GetTouch(i).position))
{
if(Input.GetTouch(i).phase == TouchPhase.Began)
{
if (grounded == true)
{
GameObject.FindGameObjectWithTag("Player").rigidbody.AddForce(Vector3.up * jumpspeed); //then find player object and move up * jumpspeed
grounded = false;//jump // set in air to grounded false
}
}
}
}
}
}
}
Answer by Owen-Reynolds · Sep 07, 2013 at 02:18 PM
From memory, position of a touch is in 0-1 screen percents, whereas mousePosition.x is in pixels. Just a matter of multiplying/dividing one or the other by Screen.width/height and using the appropriate Camera-View/Screen-ToWorld.
A pain to test mobile stuff, but a quick someGuiText.guitExt.test = Input.GetTouch(0).position.x;
can check.
Answer by stefanlaza · Sep 09, 2013 at 09:27 AM
OK when i remove grounded = false it works properly but then it's an indefinite jump any idea where can i put grounded = false so i works? Thanks for the answer!
Your answer
Follow this Question
Related Questions
TouchPhase.Began not detected. 2 Answers
When iOS framerate lags, are TouchPhase.Begans getting lost? 1 Answer
checking if grounded on rigidbody 5 Answers
I am having two errors with my jump script 1 Answer
How to shoot a raycast that checks distace between player and what's under the player? 1 Answer