- Home /
Touch the screen and jump two time with the same height
Hello,
Please i have a problem, that i cant solve since a few days. I want to make that a ball jump two time. First when we touch the screen. If the ball is grounded, it can jump and the second time when it is in the air. and with the same height.
This is my code, what i tried
void update {
if (Physics.Raycast(transform.position, -Vector3.up, raycastDistance))
{
isGrounded = true;
airCount = maxAirCount;
}
else
{
isGrounded = false;
}
if (isGrounded)
{
rb.velocity = Vector3.right * 190f * Time.deltaTime;
}
//Für Touch
if (Input.touches.Length > 0)
{
for (int i = 0; i < Input.touchCount; i++)
{
if ((Input.GetTouch(0).phase == TouchPhase.Began))
if (isGrounded)
{
rb.velocity = new Vector3(200f * Time.deltaTime, rb.velocity.y + normalJumpForce * Time.deltaTime, 0);
}
else if (airCount > 0)
{
airCount -= 1;
rb.velocity = new Vector3(200f * Time.deltaTime, rb.velocity.y + normalJumpForce * Time.deltaTime, 0);
}
}
}
}
The ball moves to the right. The height varies always. Please i need your help !!
Answer by sonu124 · Sep 18, 2015 at 10:31 AM
Try this script.....
using UnityEngine; using System.Collections;
public class BallJump : MonoBehaviour {
private CharacterController controller;
private Vector3 force;
private int count =0;
private bool isGrounded;
// Use this for initialization
void Start ()
{
controller = this.GetComponent<CharacterController> ();
force = new Vector3 (0f,250f,0f);
}
// Update is called once per frame
void Update ()
{
MouseInput ();
if(isGrounded)
{
count =0;
}
}
void OnCollisionStay(Collision col)
{
if(col.transform.tag == "Ground")
{
isGrounded =true;
}
}
private void MouseInput()
{
if(Input.GetMouseButtonDown(0))
{
count++;
if(count <2)
{
isGrounded = false;
rigidbody.velocity = Vector3.zero;
rigidbody.AddForce(force);
}
}
else if(Input.GetMouseButtonUp(0))
{
gameObject.rigidbody.useGravity = true;
}
}
}
thanks for the reply. It seems to work but not when you add for example a force, so the ball can move alone to the right, for example if you add addforce(vector3.right * 10f); and after that try to jump, the height will not be the same, anymore.
Try to apply both forces on your gameobject.. rigidbody.AddForce(Vector3.up) & rigidbody.AddForce(Vector3.right*500f)
public class BallJump : $$anonymous$$onoBehaviour {
private CharacterController controller;
private int count =0;
private bool isGrounded;
// Use this for initialization
void Start ()
{
controller = this.GetComponent<CharacterController> ();
}
// Update is called once per frame
void Update ()
{
$$anonymous$$ouseInput ();
if(isGrounded)
{
count =0;
}
}
void OnCollisionStay(Collision col)
{
if(col.transform.tag == "Ground")
{
isGrounded =true;
}
}
private void $$anonymous$$ouseInput()
{
if(Input.Get$$anonymous$$ouseButtonDown(0))
{
count++;
if(count <2)
{
isGrounded = false;
rigidbody.velocity = Vector3.zero;
rigidbody.AddForce(Vector3.up*500f);
rigidbody.AddForce(Vector3.right*500f);
}
}
else if(Input.Get$$anonymous$$ouseButtonUp(0))
{
gameObject.rigidbody.useGravity = true;
}
}
}
Already at the start, the ball is moving to the right alone, so we must have something like that
void Update ()
{
rigidbody.AddForce(Vector3.right*500f);
$$anonymous$$ouseInput ();
if(isGrounded)
{
count =0;
}
}
when i try your code, the ball is not moving alone to right direction, but only when the mouse button is down and if you add addforce like it is in the above code, the height will not be the same at the jump.
Try this one. i hope ,it will help you.
public class BallJump : $$anonymous$$onoBehaviour {
private CharacterController controller;
private int count =0;
private bool isGrounded;
// Use this for initialization
void Start ()
{
controller = this.GetComponent<CharacterController> ();
}
// Update is called once per frame
void Update ()
{
rigidbody.AddForce(Vector3.right*25f);
$$anonymous$$ouseInput ();
if(isGrounded)
{
count =0;
}
}
void OnCollisionStay(Collision col)
{
if(col.transform.tag == "Ground")
{
isGrounded =true;
}
}
private void $$anonymous$$ouseInput()
{
if(Input.Get$$anonymous$$ouseButtonDown(0))
{
count++;
if(count <2)
{
isGrounded = false;
rigidbody.velocity = Vector3.zero;
rigidbody.AddForce(Vector3.up*500f);
rigidbody.AddForce(Vector3.right*500f);
count++;
}
}
else if(Input.Get$$anonymous$$ouseButtonUp(0))
{
gameObject.rigidbody.useGravity = true;
}
}
}
Your answer
Follow this Question
Related Questions
GameObject won't jump with touch 1 Answer
How to add Jump control from touch on screen? ANDROID 1 Answer
Android issues, jump force not consistent, touch not consistent 0 Answers
how to add a speed limit? 1 Answer
Fixed Jump on android phone 0 Answers