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 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 this 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 objecthit : 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 hit : 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 something in front of the object!");
             objecthit.text = "You Hit: " + hit.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 'hit' in to the Raycast(), so it is not initialized. So your use of 'hit' 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 hit.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

what do you mean by I don't pass hit?

and how would I set the second point?

Sorry, just a weak comprehension level haha, thanks,

avatar image robertbu · Mar 26, 2013 at 03:54 AM 0
Share

There are a number of different forms of the Raycast that take different parameters. You should be using the form that take the RaycastHit as a parameter.

 Physics.Raycast(transform.position, fwd, hit, 100)

If you don't pass 'hit' as a parameter, then it does not get set. As for the second point, you would something like this inside the 'if' statement:

 lineRenderer.SetPosition(1, hit.point);
avatar image 3 · Mar 26, 2013 at 04:04 AM 0
Share

Works almost perfectly,

one more question though, if it doesn't hit any object, (just the sky) then nothing should show up, right?

Thanks,

avatar image robertbu · Mar 26, 2013 at 04:30 AM 0
Share

The way your current code is setup, the last LineRenderer settings will show up. You probably want to disable the line renderer if it does not hit anything, or you could cast a ray out a fixed distance if it misses.

avatar image 3 · Mar 26, 2013 at 04:40 AM 0
Share

alright thanks, I have an easy question for you:

http://answers.unity3d.com/questions/424881/proper-recoil-for-gun.html

youve answered a ton of $$anonymous$$e in the past, thanks

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

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