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 Hez · May 07, 2015 at 06:04 AM · c#unity 5raycast

c# Raycast going off at odd angles. Unity 5

I can't get a hit on my game object when I am standing in the red debug laser, but if I move to the left, far from the debug draw line, I get a hit. This is vexing me :( I got angry and named EVERYTHING "PlayerCube," but that did not fix anything.

 using UnityEngine;
 using System.Collections;
 
 public class Sight : MonoBehaviour {
     public Transform ObjectEye;
 
     Vector3 playerPosition;
     Vector3 subjectPosition;
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
 
         Vector3 fwd = transform.TransformDirection(Vector3.forward);
         playerPosition = transform.position;
         subjectPosition = ObjectEye.position;
         RaycastHit hit;
         if (Physics.Raycast (transform.position, subjectPosition, out hit) && hit.transform.name == "PlayerCube") {
                     
                 Debug.LogError ("poke");
 
         }
 
         Debug.DrawLine(transform.position, subjectPosition, Color.red);
 
     }
 }
 
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 joshua-lyness · May 07, 2015 at 12:19 PM 0
Share

what is the rotation of the emitter object? is it a strange value?

2 Replies

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

Answer by $$anonymous$$ · May 07, 2015 at 05:02 PM

Okey, so I finaly tested your code and it's working fine, your Raycast is correct. From the beginning it wasn't working but then I saw the rest of your Ccondition:

 if (Physics.Raycast(...) && hit.transform.name == "PlayerCube")

So I've changed the name of my target Game Object to "PlayerCube" and sudenly it worked.

Comment
Add comment · Show 4 · 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 dudester · May 07, 2015 at 05:07 PM 0
Share

glad to hear you got it working please select the correct and answer so people know which way is correct , happy coding

avatar image Hez · May 07, 2015 at 07:19 PM 0
Share

still not working, I stand right between the two objects, and no poke. The object that is raycating is turned 180, the offset seems to become greater the more it is rotated, and I need it going in a straight line.

avatar image $$anonymous$$ · May 07, 2015 at 08:09 PM 1
Share

Okey, I'm not sure if you're trying to do this effect: alt text

But if yes, this is what I've changed in Update();

 private void Update()
     {
         Vector3 Direction = ObjectEye.position - transform.position;
 
         RaycastHit hit;
 
         if (Physics.Raycast(transform.position, Direction, out hit) && hit.transform.name == "PlayerCube")
         {
             Debug.LogError("poke");
         }
         Debug.DrawRay(transform.position, Direction, Color.red);
     }

untitled.png (33.9 kB)
avatar image Hez · May 07, 2015 at 08:27 PM 0
Share

That did it! TY

avatar image
0

Answer by dudester · May 07, 2015 at 04:21 PM

this Vector3 fwd = transform.TransformDirection(Vector3.forward); should be Vector3 fwd = transform.TransformDirection(transform.forward); and then use if (Physics.Raycast (transform.position, fwd , out hit,1000f) && hit.collider.transform.name == "PlayerCube") { //something done here } Debug.DrawRay(StartPos, fwd, Color);

Comment
Add comment · Show 6 · 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 joshua-lyness · May 07, 2015 at 04:24 PM 0
Share

Haha dudester you're on the same questions as me! On a roll... lol

avatar image dudester · May 07, 2015 at 04:43 PM 0
Share

hahaha yeah im waiting on occlusion baking so nothing much else to do haha.

avatar image joshua-lyness · May 07, 2015 at 04:50 PM 0
Share

I would help you, but being the world's biggest noob has its pitfalls

avatar image dudester · May 07, 2015 at 05:00 PM 0
Share

no help needed ay just time , probably gonna take about 2 days or so to bake occlusion culling

avatar image joshua-lyness · May 07, 2015 at 05:06 PM 0
Share

Wow, your scene must be huge! Or your computer is a dell...

Show more comments

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

23 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

Related Questions

C# Raycast Code not working 2 Answers

Physics 2D Raycast is not working 3 Answers

Change Transparency 1 Answer

Need help with OnUse() script! 1 Answer

Want to move object slowly to where the mouse clicks? 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