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
1
Question by Disaster · Aug 07, 2010 at 06:17 PM · gameobjectphysicsraycast

Raycasting from the sides of a GameObject

I'm trying to figure out a way of casting rays for the sides of my player (character controller.) It's easy enough to fire a ray from the center, but for this particular task I need to be able to fire from the left and right sides of the players bounds.

I thought it would be relatively easy to get the width of the player gameObject and fire the ray from there (offset it from the center) but it seems it isn't nearly as easy I thought. Am I missing something?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Rennat · Dec 02, 2010 at 06:22 AM

I haven't tested this code but I think this method should work,

var characterController : CharacterController; var characterController = gameObject.GetComponent(CharacterController); var characterRadius : float = characterController.radius;

var rayOrigin : Vector3; var hit : RaycastHit;

// Right edge forward facing raycast rayOrigin = transform.position + transform.right * characterRadius; if (Physics.Raycast(rayOrigin, transform.forward, hit, 10)) { // Executed on right side hit }

// Left edge forward facing raycast rayOrigin = transform.position - transform.right * characterRadius; if (Physics.Raycast(rayOrigin, transform.forward, hit, 10)) { // Executed on left side hit }

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
avatar image
0

Answer by Novodantis 1 · Aug 07, 2010 at 06:28 PM

If you have the width, can you not just use that to create a temporary object at that distance either side of the centre, fire the ray from that and then delete it? Or alternatively keep the objects there to save re-creation, depending on how often it needs calculating.

Comment
Add comment · Show 3 · 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 Disaster · Aug 07, 2010 at 06:40 PM 0
Share

I don't have the width as far as I know. It isn't a property of Transform.

avatar image Novodantis 1 · Aug 09, 2010 at 06:37 PM 0
Share

Ah, apologies, so you said. Try using mesh.bounds.extents.x

avatar image Novodantis 1 · Aug 09, 2010 at 06:41 PM 0
Share

where var mesh : $$anonymous$$esh = GetComponent($$anonymous$$eshFilter).mesh;

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

No one has followed this question yet.

Related Questions

Drag Rigid Body No Distance Limit and how to add Mouselock to a scrip 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Get GameObject to face and move towards Raycast hit point 1 Answer

C# Raycast 2D GameObject follow Mouse 1 Answer

Location of raycast collision. 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