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