- Home /
GameObject won't jump with touch
When i drag my finger on the left side of the screen the gameobject moves according to the drag. But when i touch the right side of the screen(where the jump object is (invisible)) , then the gameobject stops and it doesn't jump. Can anybody help me?
void Start () {
posLine = GameObject.Find ("Main Camera").transform.position.x;
rigidbody2D = GameObject.Find("player").GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update () {
time ++;
}
void FixedUpdate(){
for(int i=0; i< Input.touchCount;i++){
if((Input.GetTouch(i).phase == TouchPhase.Moved || Input.GetTouch(i).phase == TouchPhase.Began || Input.GetTouch(i).phase == TouchPhase.Stationary))
{
myRay = Camera.mainCamera.ScreenPointToRay(Input.GetTouch(i).position);
{
if (Physics.Raycast(myRay, out rayHit ))
{
if(rayHit.collider.tag == "move")
{
for (int ii = 0; ii < Input.touchCount; ++ii) {
Touch touch2 = Input.GetTouch(ii);
if (touch2.phase == TouchPhase.Began) {
touch1 = touch2.position.x;
}
if (touch2.phase == TouchPhase.Moved || touch2.phase == TouchPhase.Stationary) {
if(touch2.position.x > touch1){
GameObject.Find ("player").transform.Translate (Vector3.right * moveSpeed * (Time.deltaTime) * 5);
}
if(touch2.position.x < touch1){
GameObject.Find ("player").transform.Translate (Vector3.left * moveSpeed * (Time.deltaTime) * 5);
}
}
}
}
if(rayHit.collider.tag == "jump"){
if (time > 70) {
GameObject.Find ("player").rigidbody2D.AddForce (transform.up * jumpPower);
time = 0;
}
}
}
}
}
}
}
}
Thanks
Comment
Answer by HYPERSAVV · Dec 18, 2014 at 02:15 AM
Start with adding a Debug.Log() statement in to at least verify that you are reaching that area of code. If you hit those statements you can assume your jump logic is sane. After that, try setting jumpForce to something crazy (ie. 5,000+)? I've often found that what I think should be a suitable amount of force is in fact no where near what it should be. Something else to try, pass ForceMode2D.Impulse as the second parameter to AddForce.