- Home /
2D game Unity5
i use Unity5
I have an object that I put the scale of it is (0.2,0.2,0.5) and I make my object double jumps. It's work
But when I adjust the size scale is (0.5,0.5,0.5), then it can not double jump. Double Jump not work i Really don't understand . help me this problem
using UnityEngine;
using System.Collections;
public class GiantScript : MonoBehaviour
{
Animator anim;
public bool grounded, doubleJump, jumping;
public Rigidbody2D rid;
public float jump;
public GameObject ground;
public LayerMask mask;
// Use this for initialization
void Start ()
{
anim = GetComponent<Animator> ();
}
// Update is called once per frame
void Update ()
{
grounded = Physics2D.Linecast (transform.position, ground.transform.position, mask);
if (grounded) {
jumping = false;
doubleJump = false;
}
if (Input.GetMouseButtonDown (0) && grounded && !doubleJump) {
jumping = true;
doubleJump = true;
rid.velocity = new Vector3 (0, 50 * jump, 0);
}
if (Input.GetMouseButtonDown (0) && !grounded && doubleJump) {
rid.velocity = new Vector3 (0, 60 * jump, 0);
doubleJump = false;
}
anim.SetBool ("GiantRun", grounded);
anim.SetBool ("GiantJump", jumping);
}
}
Answer by Ben-Stoneman · Sep 11, 2015 at 03:41 PM
@kitommy Firstly you should use FixedUpdate() when using physics in update.
I'm not 100% sure if this is the issue but you may want to check if the object is grounded in a different way.
Here is a basic script:
using UnityEngine;
using System.Collections;
public class JumpScript : MonoBehaviour
{
public float groundDistance;
Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
if (Input.GetButtonDown("Jump"))
{
bool isGrounded = GroundedCheck();
if (isGrounded)
{
rb.velocity = new Vector3(0, 10, 0);
}
if (!isGrounded)
{
rb.velocity = new Vector3(0, 2, 0);
}
}
}
public bool GroundedCheck()
{
//Checks the distance from the center of the object to the base of the object.
groundDistance = this.gameObject.GetComponent<BoxCollider>().bounds.extents.y;
//Creates a raycast to check if the ground is below this object
return Physics.Raycast(transform.position, -Vector3.up, groundDistance + 0.1f);
}
}
GroundCheck() will check if the object is grounded regardless of the size. This should rule out the issue being with the isGrounded check. If this does not fix the issue then it may be due to the jumping and doubleJump booleans.
Your answer
Follow this Question
Related Questions
how to move a puzzle piece from current position to empty position 0 Answers
Unity doesn't open mi project 0 Answers
2D EndlessRunner Spike Placing 0 Answers
HOLD THE JUMP BUTTON TO JUMP HIGHER 0 Answers
2d shooting problem 1 Answer