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 MagnusRoe · Apr 10, 2013 at 08:57 PM · cameraraycasttransformraycast

Raycast won't work after Camera transforms position. Please help!

I'm making a space ship game. I have a raycast shooting script attached to the main and only camera. It works fine and destroys the object that the raycast hits. When the player hits a wall and dies; the camera and ships transforms position to a set location at the beginning of the level. However, the raycast section of the shoot script no longer works and I have no idea why. Below is the shoot script.

using UnityEngine; using System.Collections;

 public class Shoot : MonoBehaviour
 {
     
     bool isShowingLaser = false;
     public GameObject Lazer;
     
     void Start()
     {
         Lazer.renderer.enabled = false;
         
     }
     
     void Update()
     {
         
         if(Input.GetMouseButtonDown(0))
         {            
               fireLaser();
            }            
         
     }
     
         void showLaser()
         {
            if( isShowingLaser ) return;
            isShowingLaser = true;
            Lazer.renderer.enabled = true;
            Invoke("resetLaser",0.05F);
            isShowingLaser = false;
         }
     
         void resetLaser()
         {    
                Lazer.renderer.enabled = false;
         }
     
     
         void fireLaser()
         {
             showLaser();
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); //raycast in direction of mouse position
             RaycastHit hit;
             
              if (Physics.Raycast(ray, out hit, 100.0f))
             {
             
                 if(hit.transform.gameObject.tag == "Shootable") //destroy object with tag "Shootable"
                 {
                     Destroy(hit.transform.gameObject);
                 }
         
             }    
         }
     
         
 }
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 robertbu · Apr 11, 2013 at 04:01 AM 0
Share

When you get a bug like this, put in some Debug.Log() statements to figure out what is going on. Is the raycast failing or is the raycast hitting something that is not Shootable? Output the name and the the tag inside the raycast.

 Debug.Log(hit.collider.name + " , " + hit.collider.tag);
avatar image Benproductions1 · Apr 11, 2013 at 05:21 AM 0
Share

I usually prefer to just Debug.DrawRay the ray with the distance of the hit, so I can both see what it's hitting and what direction it travels :)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by gRntaus · Apr 11, 2013 at 05:38 AM

Assuming that the object is in fact close enough to hit the object and you're still getting no luck perhaps try adding a direction to the vector in case it is going in some weird direction?

It's either something along the line of:

 if (Physics.Raycast(ray, ray.direction, out hit, 100.0f));

Or

 if (Physics.Raycast(ray, Camera.main.direction, out hit, 100.0f));

I don't have access to Unity right now but I've seen similar responses around here. I'd also suggest drawing the ray. I found when I rotated my camera that using Vector3.down still took into account the camera rotation so the ray wasn't going where I expected, I had to change the rotation accordingly.

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

12 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

Related Questions

mouse position on terrain 1 Answer

Why is the camera.screenpointtoray off? 1 Answer

Push object in direction of camera 0 Answers

is it possible to have raycasts to cover the whole screen in one Update() time? 0 Answers

How to get information from raycast? 2 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