Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by DroidifyDevs · Feb 21, 2016 at 02:19 AM · c#raycastcolliderterraindebug

When raycasting, script doesn't detect anything coming into ray yet I did everything right!

Hi!

So I've been making this script that will be used to launch missiles, and I'm wondering why when I fire the missile (NowShoot) the debug.log always says "Hit terrain" even when there is a cube clearly blocking the ray! It seems that it isn't detecting anything going along the ray. If you can't find anything wrong with my script, can you at least show some script you know works so I can modify mine? Thanks!

Here's a picture of the Debug when ray goes thru terrain:

View post on imgur.com

Now one where there is a box collider in it's way but still debug.log says "Hit terrain" :(

View post on imgur.com
 using UnityEngine;
 using UnityEngine.Networking;
 //playerweapon.cs hosts info regarding weapons
 public class Shoot : NetworkBehaviour
 {
     [SerializeField]
     private Transform LaunchPoint;
 
     [SerializeField]
     private PlayerWeapon missile;
 
     [SerializeField]
     private LayerMask mask;
 
     public void NowShoot()
     {
         //shoot the missile here;
         StartShoot();
         Debug.Log("NowShoot successful!");
     }
 
     void Start()
     {
         if (LaunchPoint == null)
         {
             Debug.LogError("PlayerShoot: LaunchPoint error!");
             this.enabled = false;
         }
         Debug.Log("Shoot.cs Started Succesfully!");
     }
 
     void StartShoot()
     {
         RaycastHit _hit;
         if (Physics.Raycast(LaunchPoint.transform.position, LaunchPoint.transform.forward, out _hit, missile.range, mask))
         {
             ShowRay();
             Debug.Log("Hit this:" + _hit.collider.name);
             //We hit something! :O
         }
         else
         {
             Debug.Log("If statement didn't complete! :( Check if all the stuff inside If() is correct");
         }
     }
 
     void ShowRay()
 
     {
         //This is to show Raycast in scene editor. This does NOT affect any in-game graphics!
         Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * 10 * 50, Color.blue);
         //Debug.Log("Ray shown! (in theory)");
     }
     
     void Update()
     {
         // need the ShowRay() to keep updating else it won't show in the scene view :(
         ShowRay();
     }
 }


Comment
Add comment · Show 1
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 dudester · Feb 22, 2016 at 06:15 PM 0
Share

look at the mask its possible your layermask is masking the collider you are blocking with , otherwise i don't see anything wrong with your script , also are you sure you calling now shoot ?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by b1gry4n · Oct 21, 2016 at 05:38 AM

If you did everything right, there wouldnt be any problems! ( i know this question is old, OH WELL ANSWERING ANYWAYS)

The ray you are drawing with debug does not look like the same ray that is being cast in your missle shot.

  Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * 10 * 50, Color.blue);

 if (Physics.Raycast(LaunchPoint.transform.position, LaunchPoint.transform.forward, out _hit, missile.range, mask))

try drawing the debug ray from the launchpoint and see if its actually in the spot you thought it was. Store the value of your launchpoint ray and update it when necessary.

 Vector3 rayStart;
 Vector3 rayDir;
 
  if (Physics.Raycast(LaunchPoint.transform.position, LaunchPoint.transform.forward, out _hit, missile.range, mask)){
     rayStart = LaunchPoint.transform.position;
     rayDir = LaunchPoint.transform.forward * missile.range;
 }
 
 void ShowRay() 
      {
          Debug.DrawRay(rayStart , rayDir , Color.blue);
      }
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

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

How to make an object move to the direction in which the user faces using vr gaze interaction? 1 Answer

Raycast doesn't interact with Tilemap Collider when in a 3D scene. 0 Answers

Raycast hit or miss in the same situation 1 Answer

I want ray cast in 4 directions, instead i am getting this. 1 Answer

C# Door Script Problem 0 Answers


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