Question by
timeismoney777 · Feb 08, 2017 at 04:00 AM ·
raycast2d gamejump
2D Raycast Script
Teach me how to jump !!
using UnityEngine; using System.Collections; using UnityEngineInternal;
public class Jumpmove : MonoBehaviour {
public float movePower = 1f;
public float jumpPower = 1f;
bool isJumping = false;
Rigidbody2D rigid;
Vector3 movement;
void Start ()
{
rigid = GetComponent<Rigidbody2D> ();
}
void Update()
{
RaycastHit2D hit = Physics2D.Raycast (transform.position, Vector2.down);
if (hit.collider.tag == "Ground" && Input.GetButtonDown ("Jump"))
{
isJumping = true;
}
}
void FixedUpdate()
{
Move ();
Jump ();
}
void Move()
{
Vector3 moveVelocity = Vector3.zero;
if (Input.GetAxisRaw ("Horizontal") < 0) {
moveVelocity = Vector3.left;
transform.localScale = new Vector3 (-1, 1, 1);
}
if (Input.GetAxisRaw ("Horizontal") > 0) {
moveVelocity = Vector3.right;
transform.localScale = new Vector3 (1, 1,1 );}
transform.position += moveVelocity * movePower * Time.deltaTime;
}
void Jump()
{
if (!isJumping)
return;
rigid.velocity = Vector2.zero;
Vector2 jumpVelocity = new Vector2 (0, jumpPower);
rigid.AddForce (jumpVelocity, ForceMode2D.Impulse);
isJumping = false;
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612110607im_/https://answers.unity.com/themes/thub/images/avi.jpg)