2D raycast only returns true.
Hi everyone I'm new to unity scripting so sorry in advance if this is a noob problem. Anyway I have a 2dgame where I'm using ray-casts to see if a character is grounded if it is it should be able to jump once using space-bar or up arrow then cannot jump again until it is grounded here is my code.
using UnityEngine;
using System.Collections;
[System.Serializable]
public class twoDPlatformer : MonoBehaviour {
public float jumpSpeed;
public BoxCollider2D trigger;
//public bool isGrounded;
public Rigidbody2D target;
public float Speed;
public bool doubleJump;
private PolygonCollider2D colide;
private float distToGround;
private int layermask;
// Use this for initialization
void Start()
{
trigger = GetComponent<BoxCollider2D>();
target = GetComponent<Rigidbody2D>();
//doubleJump = true;
colide = GetComponent<PolygonCollider2D>();
distToGround = colide.bounds.extents.y + 0.1f;
layermask = 255 >> 1;
}
// Update is called once per frame
void FixedUpdate () {
}
bool isGrounded()
{
return Physics2D.Raycast(transform.position, -Vector2.up, distToGround);
}
void Update()
{
/*if (Input.GetButton ("Right")) {
target.velocity = new Vector3(Speed,target.velocity.y,0);
}
if (Input.GetButton ("Left")) {
target.velocity = new Vector3(-Speed,target.velocity.y,0);
}
if (Input.GetButton ("Right") && Input.GetButtonDown ("Jump") && isGrounded) {
target.velocity = new Vector3(Speed,jumpSpeed,0);
}
if (Input.GetButton ("Left") && Input.GetButtonDown ("Jump") && isGrounded) {
target.velocity = new Vector3(-Speed,jumpSpeed,0);
}
if (Input.GetButtonDown ("Jump") && !isGrounded && doubleJump) {
doubleJump = false;
target.velocity = new Vector2(target.velocity.x,0);
target.velocity = new Vector3(target.velocity.x,jumpSpeed,target.velocity.y);
*/
//Ray2D myRay = new Ray2D(transform.position, -Vector2.up);
//Debug.DrawRay(transform.position, -Vector2.up, Color.black);
//Debug.Log(Physics2D.Raycast(transform.position, -Vector2.up));
Debug.DrawRay(transform.position, -Vector2.up, Color.black);
if (Input.GetButtonDown("Jump") && isGrounded())
{
target.velocity = new Vector2(target.velocity.x, jumpSpeed);
}
if(Input.GetButtonDown("Jump") && !isGrounded() && doubleJump)
{
target.velocity = new Vector2(target.velocity.x, jumpSpeed);
}
}
}
My problem is the ray-cast only returns true and idk why and when i get it not to return true (which is difficult) it doesn't work properly.
Any help would me much appreciated.
P.S Sorry about the messy code.
How do i change this i tried using layer masks, but it seems I'm not doing it right
Answer by Superfantasy · Jan 27, 2016 at 02:44 PM
Solved: I figured it out i had it hitting the collider on the character were all good now. Thank all that helped me figure out my problem.
Your answer
![](https://koobas.hobune.stream/wayback/20220612061805im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Raycast Reflection 2D 1 Answer
Raycast not detecting? 0 Answers
raycast2d problem enemies,Raycast2D problem 0 Answers