• 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 3 · Mar 26, 2013 at 02:36 AM · raycastbulletlinerendererraycasthitray

How can I can I cast a ray from a gameobject?

So I'm trying to cast a simple ray from the spawn object, because when I tried to instantiate a bullet and fire it, it didn't work. So I decided to use a raycast instead, but it doesn't seem to work. In t$$anonymous$$s scrpt I have it so that a ray is fired, and a linerenderer shows the position of where its fired, but the linerenderer doesn't move when clicked. Any help would be greatly appreciated, here is my script:

 #pragma strict
 
 var c1 : Color = Color.yellow;
 var c2 : Color = Color.red;
 var object$$anonymous$$t : GUIText;
 var lineRenderer : LineRenderer;
 function Start() { 
      lineRenderer = gameObject.AddComponent(LineRenderer);
      lineRenderer.material = new Material (Shader.Find("Particles/Additive"));
      lineRenderer.SetColors(c1, c2);
      lineRenderer.SetWidth(0.2,0.2);
      lineRenderer.SetVertexCount(2);
 }
 
  function Update () {
         var fwd = transform.TransformDirection (Vector3.forward);
         var origin = transform.position;
 var direction = transform.forward;
 var endPoint = origin + direction * 100000;
 var $$anonymous$$t : RaycastHit;
 lineRenderer.SetPosition(0, origin);
  if(Input.GetMouseButtonDown(0) || (Input.GetMouseButtonDown(1)) || (Input.GetMouseButtonDown(2))){
         if (Physics.Raycast (transform.position, fwd, 10)) {
             print ("There is somet$$anonymous$$ng in front of the object!");
             object$$anonymous$$t.text = "You Hit: " + $$anonymous$$t.collider.name;
         }
     }
     }

I thought I knew Unity well, an I'm not a beginner, but I guess I just forgot quite a bit of the basics.

Oh and I don't mean camera screen point to ray I have to have the object fired from the spawn point (the tip of the pistol) because of how I control the game.

Thanks again,

Picture of my current non functional ray

screen shot 2013-03-25 at 7.28.15 pm.png (186.0 kB)
Comment
Add comment
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

1 Reply

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

Answer by robertbu · Mar 26, 2013 at 03:33 AM

In a quick read, you have two problems. You don't pass '$$anonymous$$t' in to the Raycast(), so it is not initialized. So your use of '$$anonymous$$t' inside the 'if' statement is uninitialized. I'm surprised you did not get a runtime error. The second problem is that you never set the second point of the line renderer, so my guess is that the other end will be (0,0,0). Use a form of the Raycast() that takes a RaycastHit as a parameter, and use $$anonymous$$t.point at the second point in the LineRenderer. As a side note, take a look in the reference for 'Transform.forward.' You don't need the TransformDirection().

Comment
Add comment · Show 5 · 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 3 · Mar 26, 2013 at 03:45 AM 0
Share
avatar image robertbu · Mar 26, 2013 at 03:54 AM 0
Share
avatar image 3 · Mar 26, 2013 at 04:04 AM 0
Share
avatar image robertbu · Mar 26, 2013 at 04:30 AM 0
Share
avatar image 3 · Mar 26, 2013 at 04:40 AM 0
Share

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

10 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

Related Questions

Raycast returns null for no apparent reason 0 Answers

Need Help with RayCast. No Vector? 1 Answer

Shoot off multiple raycasts from 1 object? 2 Answers

Raycast goes through 1 Answer

Raycast visible bullets 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