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 GameDev_Chuck · Feb 05, 2015 at 12:49 PM · raycastlinecast

Issues with Linecast/Raycast

Hi, I'm making a 3rd person rpg and am attempting to implement some collision detection for the camera. I've set it up so that the camera has a child empty 1 unit to it's right. I've written a script for this empty that will linecast from the transform.position of the camera to the transform.position of the empty. If there's a hit, it's logged. Up to this point, everything works as expected.

From this point, I mirror the process and set up an empty with the same script for the left side of the camera. Now, this linecast works, but for some reason, the other one no longer works. If I disable one, the other works, and vice versa, but neither will work at the same time.

Here's the script that I've attached to the empties:

 public class CamSensor : MonoBehaviour
 {
     private Transform cameraTransform;
     
     void Start()
     {
         cameraTransform = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Transform>();
     }
 
 
     void Update()
     {
         RaycastHit hit;
         if (Physics.Linecast(cameraTransform.position, transform.position, out hit)) //if there's an object between the camera and the sensor
         {
             Debug.Log("hit from " + gameObject.name + " sensor.");   
         }
         else
         {
             Debug.Log("no hit");
         }
     }
 }

Any ideas as to why this is happening? I've also tried using Raycasts but ran into the same issue. If anyone wants to see the raycast code, just let me know and i'll put it up. As always, many thanks in advance!

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 GameDev_Chuck · Feb 05, 2015 at 01:11 AM 0
Share

$$anonymous$$inor update, I thought that having both Linecasts originate from the same point might be causing the issue so I changed that up but it did not solve the issue.

avatar image GameDev_Chuck · Feb 05, 2015 at 01:19 AM 0
Share

Update 2... For the heck of it, I tried a raycast on one side and a linecast on the other, but this didn't work either.

1 Reply

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

Answer by EvilTak · Feb 06, 2015 at 03:05 AM

Instead of putting such a script on both empty objects, remove the empty objects and add this script on the main camera:

 public float distance;
 
 void FixedUpdate() {
 Vector3 left = transform.position + transform.left * distance;
 Vector3 right = transform.position + transform.right * distance;
 
 RaycastHit hit;
 
 if(Physics.Raycast(transform.position, left, out hit)) {
 //left hit
 } 
 
 if(Physics.Raycast(transform.position, right, out hit)) {
 //right hit
 } 


Where distance is how far you want to raycast to the left and right in meters.

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 GameDev_Chuck · Feb 06, 2015 at 04:28 AM 0
Share

Ha, I was trying something like that earlier but was adding transform.left ins$$anonymous$$d of multiplying and getting the wrong behavior. I guess I need to brush up on my vector math. Thanks for the pointer :) Also, I discovered that both Linecasts were indeed working but that the debug.logs were not giving me the correct information. Thanks for taking the time to help!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

issues with jumping and linecast2d and raycast2d 0 Answers

Need help with Third person shooter 0 Answers

Pointcast? - Raycast Point X from same Point X 0 Answers

Multiple linecasts question 1 Answer

Ray/line casting for camera angle 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