• 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 CrilleStyles · Nov 01, 2015 at 08:06 PM · javascripterrorshaderraycast

Raycasting in script suddenly stopped working

Hello again my friends! So I had my code working for like 1 hour ago. And now when I start unity again my raycasting script doesn't detect my enemies anymore. And I have no idea on what to do because the code looks fine to me. I can't find any flaws. Here's the code if you want to take a look. Thank you for your help. (And I'm making a PvZ styled game FYI)

 var A : Animator;
 var EIsNear : boolean;
 
 var Arrow : GameObject;
 var ArrowPoint : Transform;
 
 var hit : RaycastHit;
  
 var Reach : float = 35.0;
 
 function Update () {
 Rayy();
 if (EIsNear == true)
     {
     A.SetBool("isAttacking", true);
     }
 
 if (EIsNear == false)
     {
     A.SetBool("isAttacking", false);
     }         
 }
 
 function Rayy()
 {
 var fwd = transform.TransformDirection (Vector3.right);
 Debug.DrawRay(transform.position, fwd * Reach, Color.red);
          
          if (Physics.Raycast (transform.position, fwd, Reach)&&hit.transform == GameObject.FindWithTag("Enemy"))
           
           {
           EIsNear = true;
           }
           else
           {
           EIsNear = false;
           }
 
 }
 

alt text

skakdasd.jpg (214.9 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Swagathaur · Nov 01, 2015 at 10:58 PM

By the looks of things your Rayy function doesnt actually change Hit. If you go to the line which calls the Physics.Raycast function and have a look at the other overloads for that function, you will see one which also takes in a RayCastHit variable. You need to add "out hit" to get it to work.

like this (I think)

Physics.Raycast(transform.position, fwd, out hit, Reach);

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

37 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

Related Questions

Problem with hitInfo in Raycast. 2 Answers

im having trouble with raycast shooting script 0 Answers

Javascript Object reference not set to instance of an object - Raycasting 0 Answers

I need help with Javascript 1 Answer

Assets/Scripts/CarCameraScript.js(35,33) '+' Cannot be used with a left hand side of type 'float' and a right hand side of type 'UnityEngine.Vector3' Help? 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