- Home /
How To Fix My RayCastHit2D in Unity 4.3.4?
Hello! I'm trying to have my 2D Player rotate towards the direction that the 2D terrain is pointing, but I'm having a problem. It only rotates/adjusts to the terrain when I jump while on the terrain. It does not rotate when I'm just moving along it. For my collision I just have 2D box colliders attached to my player along with a rigidbody2D. I don't think that's the problem since I can clearly see the colliders touching the ground. So I'm looking for some help, does anyone know what could be the problem? Long code that I wrote so I'm sorry for this: ////// = related to Raycast.
Here's my code:
[SerializeField]
GameObject TrackObject; //The Player(this game object) tracks rotation of this object in another script
[SerializeField]
Transform TerrainRot; //An empty game object that TrackObject follows rotation of.
public BoxCollider2D jumpNorm;
public BoxCollider2D jumpNorm2;
public BoxCollider2D CollideJump;
public BoxCollider2D CollideJump2;
public bool grounded = false;
public Transform groundCheck;
float groundRadius = 1.0f; //was 0.2f
public LayerMask whatIsGround;
public float jumpForce = 700f;
public Animator anim;
void Awake()
{
anim = anim.GetComponent<Animator>();
}
void FixedUpdate()
{
grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
anim.SetBool ("Ground", grounded);
anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);
anim.SetFloat ("Speed", rigidbody2D.velocity.x);
if (!grounded)
return;
}
void Update()
{
TrackObject.transform.rotation = TerrainRot.rotation; ////////
RaycastHit2D hit = Physics2D.Raycast(transform.position, -Vector2.up); ////////
Debug.DrawRay(transform.position, -Vector2.up, Color.green); ///////
if(hit.collider.tag == "Respawn") /////////
{
Debug.Log("DynamicGroundHit");
TerrainRot.transform.rotation = Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation; ////////
}
if(hit.collider.tag == "Ground") //////
{
Debug.Log("GroundHit");
TerrainRot.transform.rotation = Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation; ////////
if (AllowJump) {
if (grounded && Input.GetMouseButtonDown (0)) {
jumpNorm.enabled = false;
jumpNorm.enabled = false;
StartCoroutine (JumpCollision ());
anim.SetBool ("Ground", false);
rigidbody2D.AddForce (new Vector2 (0, jumpForce));
}
}
}
IEnumerator JumpCollision()
{
yield return new WaitForSeconds (0.1f);
CollideJump.enabled = true;
CollideJump2.enabled = true;
StartCoroutine (CollideJump ());
}
IEnumerator CollideJump()
{
yield return new WaitForSeconds(0.7f);
CollideJump.enabled = false;
CollideJump2.enabled = false;
jumpNorm.enabled = true;
jumpNorm2.enabled = true;
}
$$anonymous$$aybe your RayCast is send from the foot of the player that means exactly on the terrain and that's why it doesn't detect it? It could explain why it only works when jumping.
Thank you for replying! I think it's definitely a possibility. How can I test it or fix it? I've tried putting the raycast on an empty game object attached to the player, and changing it's position to see if that was the actual problem, but the results were not what I wanted.
I really wish I could use raycasthit2D distance(as that could be my problem), but my Unity version is too old.
To anyone who reads this post: I've seen people use a float|int on a 3D raycast to change the distance of where the raycast hits. For an example I found this code: if (Physics.Raycast(ray, out hit, 2))
The number "2" is the distance. Is it possible to do this with my 2D code here?
@Oukalakakou No i think I misunderstood it's purpose, sorry about that. But good news I found a way to make the raycast do what I want. I took some of the code from my Jump code, and put it in the FixedUpdate function. It now looks like this:
void FixedUpdate ()
{
jumper.enabled = false;
jumper2.enabled = false;
StartCoroutine (CrocJumperCollision ());
}
Although this works(and it makes my character float due to the colliders turning on/off, which I don't $$anonymous$$d), it must be highly inefficient due to running almost all the time. It also caused my game to crash on mobile. Any idea why it works, and is there a better way to do this? Thank you for your help.