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 Ochreous · Apr 30, 2013 at 05:29 PM · c#spherecast

C# SphereCast isn't Inaccurate

Hi everyone, I have been a using a SphereCast instead of a Raycast to try and make it more inaccurate. However my SphereCast doesn't seem that inaccurate and still misses the object I want to hit. Any idea what is wrong with my script?

 public float Distance = 100;
 
 public RaycastHit hit;
 
 if(Physics.SphereCast(gameObject.transform.position, 0.5f, transform.forward, out hit, Distance))
Comment
Add comment · Show 3
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 iwaldrop · Apr 30, 2013 at 05:40 PM 0
Share

Have you tried printing out what it is hitting?

 Debug.Log("Hit: " + hit.transform.name);

Generally, when using sphere casts, if the object that you are trying to hit is behind an object that the sphere cast runs into on it's path then it will hit the object that's in between the caster and the target and stop right there, returning the 'wrong' object (or, at least, not the one that you are expecting to detect in your case).

Does the target that you're trying to hit have a collider or a rigidbody? Are you also sure that it's within the 100m distance, and that your transform's forward direction is what you expect it to be? You can also try setting up your target object directly in front of your caster, and use Vector3.forward ins$$anonymous$$d of transform.forward for debugging purposes.

Essentially, Sphere-casting just works, so if you're not getting the desired results then there is something incorrect with the way that it's being implemented. :)

avatar image whydoidoit · Apr 30, 2013 at 05:46 PM 1
Share

The only problem might be the physics not being updated when you call the sphere cast if the object is moving fast.

You should try doing a Debug.DrawRay to ensure that you are pointing where you think you are.

avatar image iwaldrop · Apr 30, 2013 at 09:03 PM 0
Share

Just as long as you understand the difference between Vector3.forward and transform.forward.

Vector3.forward is in world space, meaning that it always points along the world's z axis, while transform.forward is in local space, meaning that it points along the transform's z axis.

Is your transform rotated 90 degrees along the x axis? This would cause transform.forward to point down.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Ochreous · Apr 30, 2013 at 09:09 PM

I figured it out. I switched transform.position to gameobject.transform.position since I'm using this with Unity's default FPS character controller I want it to be relevant to that gameobject.

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 iwaldrop · Apr 30, 2013 at 09:10 PM 0
Share

That effectively does nothing, because gameObject.transform and transform are the same thing. ;)

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

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

Related Questions

Saving final score and displaying on main menu 1 Answer

null texture passed to GUI.DrawTexture 0 Answers

How to Name Individual Buttons Within a List 2 Answers

c# Adjust In-Game audio 1 Answer

C# How to Detect Edges of a Collider 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