• 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
Question by ROM · Aug 03, 2010 at 09:11 PM · jointspringcatapult

Detect if object moves between two points

Basically I'm building a catapult (This style: http://www.comparestoreprices.co.uk/images/ev/everythingplay-super-sling-shot-catapult.jpg), you can currently pull it back but it needs to detect when the ball passes through the two points. Any idea how i might go about doing this? The catapult is constantly moving and twisting so I can't just say when it passes x = 100 or whatever.

Thanks in advance!

Comment

People who like this

0 Show 0
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

3 Replies

· Add your reply
  • Sort: 
avatar image

Answer by boribhi · Aug 03, 2010 at 10:18 PM

Try to place a collider ,say a box collider, at the point you want with its trigger checked, then attach a script to it like this:

function OnTriggerEnter (ball : Collider) {
    //do something here
}

Comment
Bampf

People who like this

1 Show 0 · 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

Answer by Bampf · Sep 15, 2010 at 07:25 PM

borib$$anonymous$$'s solution is a good, straightforward solution, definitely worth trying.

But note the following problem: if the ball moves very fast through the collider then it may sometimes pass completely through the collider between frames. This will happen more often the worse your frame rate is.

There are two simple ways to make this less likely. One is to simply to make the collider very deep, so the ball is less likely to pass through it undetected. The other is to reduce the physics timestep (increase the rate of FixedUpdates) in the project's settings. (This may effect overall performance of your game though.)

If you take care to test the above on slow machines OR don't care about slow machines, then you may not need to do anything else.

Otherwise, here are two ways to detect the ball no matter how bad the framerate is.

1) Use a collider as originally suggested, but add the following safety check. During each Update (or FixedUpdate) do a Linecast from the ball's previous position to the current position, and see if that ray hits the collider.

2) You can use math rather than colliders. First construct the Plane that passes through the catapult arms. Then you can use Plane.SameSide to test if the ball's previous position is on the same side of the plane as the current position. Code would look something like this:

Plane p = new Plane(catapult.transform.Forward, catapult.transform.position);
if (!p.SameSide(ballOldPosition, ballNewPosition))
{
   // do something
}
Comment
burnumd

People who like this

1 Show 2 · 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 Matthew 5 · Mar 29, 2011 at 05:50 AM 1
Share

could also make the detection continuous dynamic (assuming rigidbodies are used, which i would think they would be.

avatar image Bampf · Mar 29, 2011 at 11:13 AM 0
Share

True. Continuous and Continuous Dynamic options were added in Unity 3, and the documentation for Rigidbody covers them pretty well. Worth a look, though be aware of the performance hit.

avatar image

Answer by Mike 12 · Sep 15, 2010 at 07:28 PM

Or, you can implement something which probably uses less resources, by casting a call to Physics.Linecast library procedure. Here's what I have in mind:

    float x1 = 0.0f;
float y1 = 0.0f;
float z1 = 0.0f;
float x2 = 1.0f;
float y2 = 1.0f;
float z2 = 0.0f;
Vector3 point1 = new Vector3(x1, y1, z1);
Vector3 point2 = new Vector3(x2, y2, z2);
if(Physics.Linecast(point1, point2, MonoBehaviour.kDefaultRaycastLayers))
    // Your script has detected that something has passed through it.

Comment

People who like this

0 Show 0 · 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

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

No one has followed this question yet.

Related Questions

ConfigurableJoint simple spring 1 Answer

Why Fixed Join behaves like spring? 2 Answers

Joint confusion - Bobble head 0 Answers

First Person Slingshot 0 Answers

Strange physics behaviour with spring joints and side walls. 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