Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Golden_Gamer · Dec 17, 2018 at 10:13 PM · physicsraycastprinthit.pointdebug.drawline

Problem With RayCast

So I'm trying to have a raycast fire at the nearest target and return the object's that was hit name. I added Debug.Draw to see if it was the raycast and as far as I see the raycast doesn't do anything, not sure what I did wrong; please help.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class GrappleHook : MonoBehaviour
 {
     public RaycastHit HitInfo;
 
     private GameObject GetClosestTarget(string targetTag)
     {
         GameObject closestTarget = null;
         float closestDistance = 3.0F;
         foreach (GameObject target in GameObject.FindGameObjectsWithTag(targetTag))
         {
             float dist = Vector3.Distance(transform.position, target.transform.position);
             if (dist < closestDistance)
             {
                 closestTarget = target;
                 closestDistance = dist;
             }
         }
         return closestTarget;
     }
 
     private void Update()
     {
         GameObject ClosestTarget = GetClosestTarget("Enemy");
 
         transform.LookAt(ClosestTarget.transform);
 
         if(Physics.Raycast(transform.position, transform.forward, out HitInfo))
         {
             print(HitInfo.collider.gameObject.name);
             Debug.DrawLine(Vector2.zero, HitInfo.point); //Doesn't Draw Anything
         }
 
     }
 }
 
 
Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Vega4Life · Dec 18, 2018 at 12:29 AM

If you want to see where the raycast is visually, debug outside the raycast


 Debug.DrawLine(transform.position, transform.forward, Color.red);
 if(Physics.Raycast(transform.position, transform.forward, out HitInfo))
          {
              print(HitInfo.collider.gameObject.name);
          }



This will draw a red line from your character to 1meter forward - which is the exact length and direction of your raycast.


Comment
Add comment · Show 5 · 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
avatar image Golden_Gamer · Dec 18, 2018 at 08:58 PM 0
Share

Thx you, but it still doesn't do anything. I moved Debug.DrawLine like you said, but it still has no effect.

avatar image Vega4Life Golden_Gamer · Dec 18, 2018 at 09:46 PM 0
Share

It doesn't draw in game view, only the scene view.

avatar image Vega4Life Vega4Life · Dec 18, 2018 at 09:48 PM 0
Share

You should also probably make it longer because the transform gizmo arrows are 1meter. So do this


 Debug.DrawLine(transform.position, transform.forward * 5f, Color.red);

avatar image Golden_Gamer · Dec 19, 2018 at 06:08 PM 0
Share

THX U, IT WOR$$anonymous$$S NOW. I don't know how, but I deleted all my code and rewrote it and it worked now (with that change of course).

avatar image Vega4Life Golden_Gamer · Dec 19, 2018 at 06:11 PM 0
Share

Sure. Good luck!

avatar image
0

Answer by Cornelis-de-Jager · Dec 18, 2018 at 09:09 PM

I think it is drawing but just too short to see because you added no distance. Maybe.

Try the code below:

 private void Update()
     {
         GameObject ClosestTarget = GetClosestTarget("Enemy");
  
         transform.LookAt(ClosestTarget.transform);
         
         Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * 2, Color.white);
 
         if(Physics.Raycast(transform.position, transform.forward, out HitInfo))
         {
             Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * 1000, Color.Red);
         }
  
     }
 }
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
avatar image
0

Answer by Golden_Gamer · Dec 18, 2018 at 11:50 PM

Oh it's only in the scene view, size isn't a problem; it went as far as I could see, but now I know the raycast is what is wrong with the code, it wont follow the closest target like I want. Thank You For The Help.

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

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

Raycast not working on build 0 Answers

How to find what type of tile player is on in 2D game 1 Answer

Can't raycast on Character Controller? 1 Answer

detection raycast too late 2 Answers

RayCast Nearest Character 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