Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Goodmanson · Mar 31, 2016 at 03:34 AM · gameobjectgameobject.tag

Problems with Physics.Raycast

I’m making a small game that mainly just involves picking up and throwing things. To do so, I’m trying to create a Raycast on the player, so that when he gets close enough to something, it’ll show that he can pickup and throw it.

Here's the code:

 #pragma strict
 
 function OnCollisionEnter(col : Collision) {
 
     var fwd: Vector3 = transform.TransformDirection(Vector3.forward);
 
     if (Physics.Raycast(transform.position, fwd, 5) && col.gameObject.tag == "Enemy")
      {           
           print("Put em' in the traaaaaaaaaash");
 
        }
      
 }

The problem is that whenever I go up to the object with the "Enemy" tag, nothing shows up on the console. What am I doing wrong?

Comment
Add comment · Show 6
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 ZefanS · Mar 31, 2016 at 07:54 AM 1
Share

It's not exactly clear what kind of behaviour you're trying to achieve, but right now your script works like this: OnCollisionEnter() is called during the frame in which you make contact with the enemy. When this happens you send out a raycast. If that hits the enemy then you print to the console. What this means is that the player will have to be directly facing the object while colliding with it. I was able to get the line to print in the console, but it required some finesse with the controls.

The best way to change the script depends on how you want your pickup mechanic to work. Right now you could summarize it like this: "To pick up an object, look directly at it while walking into it." I would say that's not that intuitive and would suggest some possible alternatives such as:

  • "To pick up an object, walk into it."

  • "To pick up an object, look directly at it and click the Left $$anonymous$$ouse Button."

  • "To pick up an object, walk near it and press the Action $$anonymous$$ey."

There are of course many, many other ways you could do this as well. If you could describe exactly what you want to happen, then it would be easier to suggest some solutions.

avatar image Goodmanson ZefanS · Mar 31, 2016 at 01:23 PM 0
Share

I want it so that when you walk up close and look at the game object with the tag "Enemy" for you to be able to then click and be able to pick it up and throw it, but right now, I just want the message to show up on the console. What am I doing wrong?

avatar image ZefanS Goodmanson · Apr 01, 2016 at 12:31 AM 0
Share

Since you just want to the player to have to be close to, but not actually touching the object, you don't need to use OnCollisionEnter() or any other collision detection. You only need to check that the raycast can hit the object. Since you're already limiting the length of the ray to 5m, this will effectively make it only possible to interact with the object at close range.

For now, you can probably just throw that raycasting code into Update() for testing purposes, but more ideally you'll want to test for mouse clicks before doing the raycast because you won't want to be raycasting every frame.

Also, @TanselAltinel is right; you can just use transform.forward ins$$anonymous$$d of doing the whole thing with TransformDirection().

Show more comments
avatar image TanselAltinel · Mar 31, 2016 at 01:50 PM 1
Share

Why are you not using transform.forward ?

0 Replies

· Add your reply
  • Sort: 

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to Respawn an gameObject when it touch another gameObject 1 Answer

How could I use a GameObject array to check the associated tags and toggle their Box Colliders accordingly? 1 Answer

GameObject is already being activated or deactivated 3 Answers

Destroyed instance of Prefab, can't spawn it back. 1 Answer

Reset rotation of gameobject 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