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 Koron321 · Jan 21, 2015 at 11:59 AM · javascriptraycastfunctiondistanceraycasthit

Raycasts don't go the right direction unless i'm really far away (javascript)

I've got a very basic fps made, there are cubes that follow you around and lower your health if they touch you and you have a gun that you can kill them with (which i'm using raycast for) but I've run into a problem, my gun only works if I'm really far away from the cubes. if i get close at all, the ray doesn't go forward, which i can't figure out because I'm using transform.forward. This is the shoot function:

 function Shoot () {
     Debug.DrawRay(transform.position, CamScript.target, Color.red, 20, false);
     var hit : RaycastHit;
     if (canShoot == true) {
         canShoot = false;
         if (Physics.Raycast(transform.position, transform.forward, hit)) {
             if (hit.transform.tag == "Enemy") {
                 hit.transform.SendMessage ("TakeDamage");
             }
         }
         yield WaitForSeconds (10 / fireRate);
         canShoot = true;
     }
 }
Comment
Add comment · Show 7
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 meat5000 ♦ · Jan 21, 2015 at 11:36 AM 0
Share

Your DrawRay and Raycast dont match. How can you be sure?

Create a variable of type Ray and set up what you want your ray to be in there. Use this for both the raycast and debug.

avatar image Koron321 · Jan 22, 2015 at 01:29 AM 0
Share

Can you explain how the drawray is wrong? not disagreeing just curious, CamScript.target is the point the gun points towards... sry for the late response, i have school but i should respond fast later in the day, trying wat u said now.

avatar image Koron321 · Jan 22, 2015 at 01:42 AM 0
Share

ya I can't figure out how to use the ray variable type... never done it like this before, when i try to do it like in the documentation it calls the ray variable "System.Type", but it should be something like "UnityEngine.Ray" right?

avatar image Eric5h5 · Jan 22, 2015 at 02:49 AM 0
Share

DrawRay draws rays, which have a position and a direction, like a raycast. You have two positions; that's not a ray.

avatar image Koron321 · Jan 22, 2015 at 02:54 AM 0
Share

ok that makes sense, i changed it to transform.forward and now it looks like the ray is going backwards, but only when i'm close to the cube

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by Inok · Jan 22, 2015 at 08:41 AM

Try instead "transform.forward" use "transform.TransformDirection(Vector3.forward)". Ray go backward when enemy close to you because start position of ray inside enemy's collider at that moment.

Comment
Add comment · Show 1 · 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 Eric5h5 · Jan 22, 2015 at 05:28 PM 0
Share

transform.forward is relative forward and is the same as transform.TransformDirection(Vector3.forward), except cleaner and easier to read.

avatar image
0

Answer by team_eden · Jan 22, 2015 at 10:20 PM

then the position of your game object is reaching behind the cube? you need to set an anchor for your ray which is not part of your main object, as it apparently reaches farther away than the position of your actual intended object.

Put a blank object on the muzzle of your gun, called muzzleRayAnchor. Now, assuming your model is pointing forward if (Physics.Raycast(muzzleRayAnchor.transform.position, muzzleRayAnchor.transform.forward, hit))

It is important to set the forward of the muzzle Anchor, as it is the object that is staying in alignment with your gun, or what have you. Hope that helps

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

RaycastHit.distance 1 Answer

Why is this Coroutine not working ? 1 Answer

Raycast Distance Help 1 Answer

"NullReferenceException: Object reference not set to an instance of an object" When Raycast hits and tries select a designated object 2 Answers

When I click, my enemy doesnt lose health... 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