• 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
1
Question by sona.viswam · Feb 18, 2013 at 04:02 AM · collisionobjectray cast

Raycast hit in unity 3d

i have a character which is moving in z direction with constant velocity.

lots of obstacle objects are occupied infront of character.

while user touches on obstacle objects infront of the character should get shooted.

camera is moving with character .

camera is child of character.

i used raycast to detect objects.

My problem is that sometimes it get destroys , and sometimes not.

Raycast not hit is my exact problem.

 if(Input.GetMouseButtonDown(0)){  
 Ray ray = camera.ScreenPointToRay(Input.mousePosition); 
 if (Physics.Raycast (ray, out hit3, 1000.0f)){
 Destroy(hit3.collider);
 }
 }

camera is not in (0, 0, 0) angle.

help with good solution.

Is it better to give raycast to character or camera?

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 Benproductions1 · Feb 18, 2013 at 04:26 AM 1
Share

Please properly indent your code

avatar image sona.viswam · Feb 18, 2013 at 06:14 AM 0
Share

@Benproductions: which portion u get confused

1 Reply

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

Answer by Benproductions1 · Feb 18, 2013 at 04:25 AM

Hi!

Firstly, writing out in the parameter of the Raycast Function iis just plain wrong, get rid of that! Secondly you are deleting the collider, which is a component of a gameObject. I'm surprised it even destroyed anything. Instead you should delete the gameObject of the collider or transform.

So: Destroy(hit3.transform.gameObject); is the way

Hope this fixes it, Benproductions1

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 sona.viswam · Feb 18, 2013 at 05:52 AM 1
Share

am sorry. my raycast not reach the object properly. i dont want destroy game object. just want to change texture. main issue is raycast. this issue blessed with sleepless nights

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

Var CollisionObject not working. 1 Answer

import google sketchup collision 1 Answer

Basic Collision, Object + First Person 2 Answers

Move object to raycast point. 3 Answers

Player collision activates GO 0 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