Question by
Manacraft05 · Dec 27, 2018 at 08:49 PM ·
2draycast
Raycast 2d don't work
hey please help me, I need to access the distance between the ground : the code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class player : MonoBehaviour {
public Rigidbody2D playerrigid;
public Transform playertransform;
public float groundDistance; // the var I want to put the distance in
void Start () {
}
// Update is called once per frame
void FixedUpdate () {
RaycastHit2D hit = Physics2D.Raycast(playertransform.position, -Vector2.up); // raycast to the ground
groundDistance = hit.distance; // set the variable
playertransform.Rotate(0, 0, Input.GetAxis("Vertical")); // player rotation
}
}
Comment
Are your public variables populated? playertransform isn't null correct? You've added the correct transform in the inspector? Is the RaycastHit2D empty?
put
print(hit);
Just after you fire the ray and see what the rayHit contains. If it's null let me know if not maybe a trigger is in the way or something. Anyhow, start there.
Your answer
Follow this Question
Related Questions
Two Raycast2D on object cancel each other out 0 Answers
Feild of view only wokring in specific areas of the scene 0 Answers
Inconsistent raycast2D reflecting 0 Answers
Want to do an on-hit effect for a trail renderer in 2D 0 Answers
Raycast on mouse position problem. how not to raycast to 0 1 Answer