- Home /
Weird Rayscast-Distances
Hello,
Based on some tutorial-material i made a small terrain to play arround and imported a first-person-controller. Now I've added an empty GameObject as a child to the Main Camera and added this script to determine the distance to certain objects:
using UnityEngine;
using System.Collections;
public class MeleeSystem : MonoBehaviour {
public int Damage = 10;
public float Distance = 0;
public float MaxDistance = 1.5f;
public Axe Weapon;
public AudioClip hitsound;
// Use this for initialization
void Start () {
setMaxDistance (Weapon);
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown ("Fire1"))
{
RaycastHit hitRayCast;
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward),Color.blue, 30);
if(Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hitRayCast, Mathf.Infinity))
{
Distance = hitRayCast.distance;
if(Distance <= MaxDistance)
{
audio.PlayOneShot(hitsound);
Weapon.animation.Play("AxeHit");
}
}
}
}
void setMaxDistance(Axe tool)
{
MaxDistance = tool.Length + 1.5f;
}
void OnGUI()
{
GUI.Box(new Rect(Screen.width/2,Screen.height/2, 10, 10), "");
}
}
As I said, I'm just starting to play arround... Right now I have the problem, that the values don't make sense to me. When i look down straight, I get 0.12 as distance, but when I highen up the angle just a little I get 5.82... I've added an screenshoot to ilustrate my problem.
Can someone please explain this to me? Google didn't help so far.
Greetings
I think your Ray may be hitting you Player Transform. You can check the name of the object which is hit: RaycastHit.hit.transform.name
If you do hit your Player, you should use layers and layermasks to make your Ray ignore your Player Transform http://answers.unity3d.com/questions/8715/how-do-i-use-layermasks.html
thanks for the hint, you were right with the "player-hit-thing", but that doesn't solve the question why the terrain is 3.x units away. How do those units count? Since the $$anonymous$$ain Camera is only 0.9 Units high that seems pretty far away to me...
when i look straight down (i've made the fps-controller-scale to 0 in x and z) the terrain is still 2.23 units away. how can that be, when the Camera is just 0.9 units above the ground (since the fps-controller stands right on it).
Answer by Husky110 · May 02, 2014 at 11:22 PM
I finally got it figured out... Since the camera doesn't follow an 90° angle (it can only handle 60°) the distance in this case is calculated by the height of the FPS-controller + the height of the Main Camera-Component. In my case the FPS-controller had a height of 1,05 and the Main Camera-component a height of 0,90 which made a total height of 1,95 units. Therefore the distance became a hypthenuse of a triangle. (I leave this here, so future newbies like me can figure it out more specificly, because this approach only gave the rough values.)
Your answer
Follow this Question
Related Questions
can i make a raycast only detect one collider? C Sharp 1 Answer
C# Check Physics.Raycast Once 0 Answers
AI Shooting for Aircraft 1 Answer
Determine object hitting a static collider at update() 1 Answer
A node in a childnode? 1 Answer