- Home /
Question by
EpoOL_89 · Apr 08, 2014 at 04:35 AM ·
jumpsimultaneously
make character jump like frog
Greeting all... I am totally beginner here... I want to make some game that when we push jump button, the character will jump and move simultaneously (like a frog jumping)... so far i have done with some code which is only make it jump up only... i want it move to the right as well with one pushed button... really2 appreciate for ur helping... :)
Here's my code :
using UnityEngine; using System.Collections;
public class jumpControl : MonoBehaviour {
[HideInInspector]
public bool jump = false;
public float moveForce = 365f;
public float maxSpeed = 5f;
public float jumpDistance ;
public float jumpForce = 1000f;
private Transform groundDetector;
private bool grounded = false;
void Awake()
{
// Setting up references.
groundDetector = transform.Find("GroundDetector");
}
void Update()
{
// The player is grounded if a linecast to the groundcheck position hits anything on the ground layer.
grounded = Physics2D.Linecast(transform.position, groundDetector.position, 1 << LayerMask.NameToLayer("Pod_Sprite"));
// If the jump button is pressed and the player is grounded then the player should jump.
if(Input.GetButtonDown("Jump") && grounded)
jump = true;
}
void FixedUpdate ()
{
// Cache the horizontal input.
float h = Input.GetAxis("Horizontal");
if(h * rigidbody2D.velocity.x < maxSpeed)
rigidbody2D.AddForce(Vector2.right * h * moveForce);
if(Mathf.Abs(rigidbody2D.velocity.x) > maxSpeed)
rigidbody2D.velocity = new Vector2(Mathf.Sign(rigidbody2D.velocity.x) * maxSpeed, rigidbody2D.velocity.y);
// If the player should jump...
if(jump)
{
rigidbody2D.AddForce(new Vector2(30f, jumpForce));
jump = false;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Jumping through the roof! 1 Answer
How to make smooth jump? 1 Answer
Simple wall jump 0 Answers
2D box collider (character floating) 1 Answer