Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Husky110 · May 02, 2014 at 04:14 PM · c#physicsraycast

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.

alt text

Can someone please explain this to me? Google didn't help so far.

Greetings

unbenannt.png (256.4 kB)
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Simon-Larsen · May 02, 2014 at 04:29 PM 0
Share

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

avatar image Husky110 · May 02, 2014 at 04:57 PM 0
Share

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...

avatar image Husky110 · May 02, 2014 at 05:01 PM 0
Share

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).

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges