• 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 sinkler747 · Mar 15, 2013 at 05:55 PM · raycastshoot

my ray cast is only shooting out a few meters no matter the distance.

my ray cast is only shooting out a few meters no matter the distance. currently distance is at a 1000 units( i should say) the pic is yellow is he ray cast line drawn. the second pc i believe it is in blue

alt text

I have a function and it has been assigned to the update function. when i debug it, it does tell me that the range is current at 1000, but the ray only shoots a few meters. below is the function, and below that is the update function that may or may not be temporary. although the function ShootContrllor is displayed first, it is actually below, a few other functions down.

please be advised that i watched the unity answers video, and i have done accordingly. please do the same and remember this is unity answers and not unity forum. In particular pointing me to some reference on raycasting, or .....you know.

 public void ShootController()
     {
         
 
                 currentWeapon.shotTimer = Time.deltaTime + currentWeapon.firerate;
                 Debug.Log("I shot");
 
             
                 RaycastHit hit;
                 if(Physics.Raycast(currentWeapon.SpawnPoint.position, currentWeapon.SpawnPoint.TransformDirection(Vector3.forward), out hit,currentWeapon.weaponRange))
                 {
                     Debug.DrawRay(currentWeapon.SpawnPoint.position, currentWeapon.SpawnPoint.TransformDirection(Vector3.forward),Color.blue,currentWeapon.weaponRange);
                     
                     if(hit.collider.gameObject.name == GameObject.FindWithTag("HeadCollider").gameObject.name)
                     {
                         Debug.Log("I hit the Head");    
                         Debug.Log("Range is" + currentWeapon.weaponRange);
                     }
                     else if(hit.collider.gameObject.name == GameObject.FindWithTag("BodyCollider").gameObject.name)
                     {    
                             Debug.Log("I hit the Body");    
                             Debug.Log("Range is" + currentWeapon.weaponRange);
                     }
                     else
                     {
                         Debug.DrawRay(currentWeapon.SpawnPoint.position, currentWeapon.SpawnPoint.TransformDirection(Vector3.forward),Color.yellow,currentWeapon.weaponRange);
                         //Debug.Log("I hit something");
                     }
                 
 
             }
 
         
     }








 public void Update()
 {
     if(currentWeapon.shotTimer > 0)
     {
         currentWeapon.shotTimer -= Time.deltaTime;    
     }
     
     if(currentWeapon.shotTimer < 0)
     {
         currentWeapon.shotTimer = 0;    
     }
     
     if(Input.GetMouseButtonDown(0) && currentWeapon.bulletsLeft > 0)
     {
         if(currentWeapon.shotTimer == 0)
         {
             ShootController();    
             
         }
         
         Bullets();
         
     }
     

     
 }
screen shot 2013-03-15 at 11.44.03 am.png (431.2 kB)
screen shot 2013-03-15 at 12.12.29 pm.png (499.0 kB)
Comment
Add comment · Show 2
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 sinkler747 · Mar 15, 2013 at 06:52 PM 0
Share

Thanks for leading me to the error, although the docs were trial and error. they are written for professionals by professionals and aren't detailed enough for the layman. i got it though.......... thumbs up

 Vector3 forward = currentWeapon.SpawnPoint.TransformDirection(Vector3.forward);
     
  if(Physics.Raycast(currentWeapon.SpawnPoint.position, forward * currentWeapon.weaponRange, out hit,currentWeapon.weaponRange))
                     {
                          
                     Debug.DrawRay(currentWeapon.SpawnPoint.position,forward *           currentWeapon.weaponRange,Color.blue,currentWeapon.weaponRange);
avatar image Julien-Lynge · Mar 15, 2013 at 06:56 PM 0
Share

Awesome, good luck with your game!

1 Reply

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

Answer by Julien-Lynge · Mar 15, 2013 at 05:57 PM

Here is your error:

 Debug.DrawRay(currentWeapon.SpawnPoint.position, currentWeapon.SpawnPoint.TransformDirection(Vector3.forward),Color.yellow,currentWeapon.weaponRange);

You are drawing a ray that is only 1 unit long (Vector3.forward is a unit vector).

Please see the script reference:

http://docs.unity3d.com/Documentation/ScriptReference/Debug.DrawRay.html

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

11 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

Related Questions

How to get a rayhit to detect tags? 1 Answer

Raycast hitting below mouse position :( 0 Answers

How can i tell the distance from a raycast? 1 Answer

How to make mobile controls shoot prefab bullets 0 Answers

Shoot Projectile 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