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 keyp3r · Dec 05, 2013 at 04:49 AM · collisionphysicsfixedupdatespherecast

Spherecast not always returning hits when it should?

In the game I'm working on I have a beam-type weapon, where the beam is pretty large. I assumed the best way to do this would be a spherecast but it only seems to function about 2/3 of the time it should be. It will clearly be colliding with objects, over the course of up to a second, before it registers a hit on the Spherecast (running on FixedUpdate). Theres really not much to what I'm doing, but just in case...

 public float Distance=20;
 public float ExtendSpeed=10;
 public float Width=2.5f;
 public LayerMask HitMask = new LayerMask();
 public Vector3 endPos;
 
 void FixedUpdate(){
     endPos = Vector3.Lerp(endPos,transform.position+(Vector3.forward*Distance),Time.deltaTime*ExtendSpeed);
     endPos.x = transform.position.x;
     
     Ray ray = new Ray(transform.position,endPos);
     RaycastHit hit;
     if(Physics.SphereCast(ray,Width,out hit,Distance,HitMask)){
         HitObject(hit.collider.gameObject);
         updateTimer=0;
         endPos.z = hit.point.z + 1;
     }
 }
 
 void HitObject(GameObject hit){
     Debug.Log("I hit a '" + hit.name + "'!");
 }

And yes, the LayerMask is set up properly. I mostly only have one so the spherecast doesn't hit the player object at all. Thanks for anything you guys can help with!

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 · Dec 05, 2013 at 05:10 AM

A Ray is a position and a direction. You are using two positions to construct your ray. I assume you are using Lerp() so that the beam happens over time rather than all at once. But your use of Lerp() here will produce an eased movement, so your calculation will dramatically slow and potentially take a lot more time than you anticipate to get near the final position. You can fix this by changing to MoveTowards(). But the reality is you are producing some sort of strange sweep rather than a beam. And since 'Distance' remain constant, you are not shooting over time. To figure out what is going on add this at Line 10:

 Debug.DrawRay(transform.position, endPos * Distance); 

And I'm not sure why you have this line:

 endPos.x = transform.position.x;

Vector3.forward() is (0,0,1), so it will not change the 'x' position of your beam, so as far as I can see, this line is a no-op.

Finally make sure the leading edge of your spherecast starts inside your character.

Comment
Add comment · Show 4 · 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 robertbu · Dec 05, 2013 at 05:20 AM 0
Share

I think you are looking for something more like this:

 public float distance = 20;
 public float extendSpeed = 10;
 public float width = 2.5f;
 public Layer$$anonymous$$ask hit$$anonymous$$ask = new Layer$$anonymous$$ask();

 private float length;
 private float maxLength;

 void Start() {
     length = 0;
     maxLength = distance;
 }
 
 void FixedUpdate(){
     length = length + Time.deltaTime * extendSpeed;
     if (length > maxLength)
         length = maxLength;
     
     Ray ray = new Ray(transform.position,Vector3.forward);
     RaycastHit hit;
     if(Physics.SphereCast(ray, width, out hit, length, hit$$anonymous$$ask)){
         //HitObject(hit.collider.gameObject);
         //updateTimer = 0;
         maxLength = length;
     }
 }

You'll have to reset 'length' and 'maxLength' each time you fire.

avatar image keyp3r · Dec 05, 2013 at 05:47 AM 0
Share

This is a snippet from some much larger code, hence why the question was centric to ShpereCasts and not my logic. I'm A$$anonymous$$ using/expecting a sweeping motion which is why I'm using a Lerp. I didn't realize that with the Ray though, I'll adjust my code and get back to you. Thanks!

avatar image robertbu · Dec 05, 2013 at 06:06 AM 1
Share

I'm not sure of your sweeping calculations, but if everything else is okay, then all you need to do is subtract transform.position from endPos to get your direction vector for the ray.

avatar image keyp3r · Dec 05, 2013 at 06:09 AM 0
Share

Yeah, thats what I just found haha. This is what it looks like now:

 public float Distance=20;
 public float ExtendSpeed=10;
 public float Width=2.5f;
 public Layer$$anonymous$$ask Hit$$anonymous$$ask = new Layer$$anonymous$$ask();
 public Vector3 endPos;
  
 void FixedUpdate(){
     endPos = Vector3.Lerp(endPos,transform.position+(Vector3.forward*Distance),Time.deltaTime*ExtendSpeed);
     endPos.x = transform.position.x;
  
     Ray ray = new Ray(transform.position,Vector3.Normalize(endPos-transform.position));
     RaycastHit hit;
     if(Physics.SphereCast(ray,Width,out hit,Distance,Hit$$anonymous$$ask)){
        HitObject(hit.collider.gameObject);
        updateTimer=0;
        endPos.z = hit.point.z + 1;
     }
 }
  
 void HitObject(GameObject hit){
     Debug.Log("I hit a '" + hit.name + "'!");
 }

And it works great. = D Thanks for pointing out the Ray thing, glad it was an easy fix.

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

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

Related Questions

OnCollisionEnter inside FixedUpdate? 1 Answer

Force physics collision calculation (forced fixed update) 0 Answers

How do you perform a stationary SphereCastAll? 1 Answer

2D Collider aren't exact? 6 Answers

Keep Horizontal Momentum after Jump 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