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 Guidez · Feb 25, 2013 at 05:14 AM · collisionphysicsraycast

Physics required for collision detection?

So here is my scenario:

http://www.youtube.com/watch?v=W7eG28MBK6I

I would like the paddle to NOT be able to pass through the walls/bricks. I have tried variations/combinations of rigid bodies, triggers, etc, but did not get the desired effect (notably the paddle being affected by phsyics that I didn't want).

I remember reading a snippet somewhere about using RayCasting, especially in the case of fast moving objects, so perhaps the better way to go is with that? If so, no idea where to start from xD

Code: PaddleScript.js

 #pragma strict
 
 //***********
 //Variables
 //***********
 var Ball : GameObject;
 var paddleSpeed : float = 10;
 var codeBall : GameObject = null;
 var inGameLives = 4;
 //***********
 
 function Start () {
     //Spawns the ball at the beginning of the games with the SpawnBall function
     SpawnBall();
 }
 
 function SpawnBall() {
     //Spawns ball
     codeBall = Instantiate(Ball, transform.position + Vector3(0,.475,0),Quaternion.identity);
     inGameLives = inGameLives - 1;
 }
 
 function Update () {
 
     //Moves paddle left/right/up/down
     transform.Translate(paddleSpeed * Time.deltaTime * Input.GetAxis("Horizontal"),paddleSpeed * Time.deltaTime * Input.GetAxis("Vertical"),0);
 
     //Keeps the paddle from moving below the terrain
     if (transform.position.y < -5.5){
         transform.position = Vector3(transform.position.x,-5.5, transform.position.z);
     }
     
     //Keeps the paddle from moving beyond the right wall. Needs to be fixed
     if (transform.position.x > 6.50) {
             transform.position = Vector3( 6.50, transform.position.y, transform.position.z);
         }
     //Keeps the paddle from moving beyond the left wall. Needs to be fixed    
     if (transform.position.x < -6.50) {
             transform.position = Vector3( -6.50, transform.position.y, transform.position.z);
         }
     
     //Launches the ball at the start of the game, as long as "codeBall" proves true first
     //Also moves the ball with the paddle prior to launching
     if (codeBall){
         codeBall.rigidbody.position = transform.position + Vector3(0,.475,0);
             if (Input.GetButton("Launch")) {
             codeBall.rigidbody.isKinematic = false;
             codeBall.rigidbody.AddForce(300 * Input.GetAxis("Horizontal"),300,0);
             codeBall = null;
         }
     }
     
     //Moves the camera along with the paddle on the Y Axis
     Camera.mainCamera.transform.position = Vector3(0,transform.position.y,-10);
 }
 
 function OnCollisionEnter(paddleHitSomething : Collision) {
     //Alters the direction of a ball when it hits the paddle based on where it hit the paddle
     if (paddleHitSomething.gameObject.tag == "Ball"){
         for (contact in paddleHitSomething.contacts){
             if (contact.thisCollider == collider) {
                 var paddleHit = contact.point.x - transform.position.x;                
                 contact.otherCollider.rigidbody.AddForce( 300f * paddleHit, 0, 0);
             }
         }
     }
 }

Bonus help: Any idea on how to keep the ball better "attached" to the paddle?

And yes: I love to comment my code :D

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
0

Answer by nsxdavid · Feb 25, 2013 at 07:55 AM

There are many ways to approach this. If you want to use Physics, since are for the game itself I presume...

Attach a box collider to the paddle and size appropriately. Attach a rigidbody and set it to kinematic.

Move your code in Update() over into FixedUpdate().

Keep track of the last valid position the paddel was in by storing this on a variable on the script. Update at the top of FixedUpdate().

When you get a collision on the paddle (that isn't the ball), move the paddle back to the stored last valid position.

Your ball should also be better attached by using FixedUpdate() instead of Update().

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 Guidez · Feb 26, 2013 at 06:28 PM 0
Share

So fixed update did the trick for keeping the ball better attached.

As to the paddle though, still a no go. Attached the kinematic rigidbody, but:

For my script variables, I created:

 var lastPaddlePosition : Vector3;

Then:

 function FixedUpdate () {
 
     lastPaddlePosition = Vector3(transform.position.x,transform.position.y,transform.position.z);

Then:

 function OnCollisionEnter(paddleHitSomething : Collision) {
     if (paddleHitSomething.gameObject.tag == "Brick"){
         Debug.Log ("Something was hit!");
         //transform.position = lastPaddlePosition;
     }

Neither the debug nor the transform (uncommented) did anything. I thought !paddleHitSomething.gameObject.tag == "Ball" would do the trick as well, but for whatever reason the paddle is still passing through things.

avatar image Guidez · Feb 26, 2013 at 07:10 PM 0
Share

Ok, I got further now: I added a non-kinematic rigidbody to my "brick", and froze all the constraints (they were spinning off into oblivion). The debug-log now kicks off as it should, the bricks don't go flying off, but for whatever reason my paddle is still passing through the bricks... Hmmm...

P.S. Thank you for not just replying with "Here is what your code should look like". It's good learning to get the hints and then figure out the exact code myself :D

avatar image Guidez · Feb 26, 2013 at 08:39 PM 0
Share

Sigh, but now my ball get's "stopped" by the bricks ins$$anonymous$$d of bouncing off of them... What a bother...

avatar image Guidez · Feb 27, 2013 at 03:24 AM 0
Share

Any help would be nice... I've been able to incorporate the "DontGoThroughThings" script, and even have it sending messages to debug correctly, but for whatever reason I can still pass through things!

avatar image
0

Answer by Guidez · Feb 28, 2013 at 04:49 AM

The issue with my code was that I was setting the transform position instead of the rigidbody position, with transform performing no collision detection.

Besides the revised code for the paddle below, I also had to increase the "paddleSpeed" from 14.5 to 600, and had to have a non-kinematic rigid body on the paddle with Z Position + XYZ Rotation constraints. The ball is no longer staying exactly on top of the paddle, but I will fix that later. I also used velocity instead of MovePosition on the rigidbody, as there was some force being added to the paddle on MovePosition I didn't like

Note: Earlier answer had me move everything to FixedUpdate for keeping the ball better attached.

New PaddleScript.js

 #pragma strict
 
 //***********
 //Variables
 //***********
 var Ball : GameObject;
 var paddleSpeed : float = 600;
 var codeBall : GameObject = null;
 var inGameLives = 4;
 //***********
 
 function Start () {
     //Spawns the ball at the beginning of the games with the SpawnBall function
     SpawnBall();
 }
 
 function SpawnBall() {
     //Spawns ball
     codeBall = Instantiate(Ball, transform.position + Vector3(0,.475,0),Quaternion.identity);
     inGameLives = inGameLives - 1;
 }
 
 function FixedUpdate () {
 
     //Moves paddle left/right/up/down
     rigidbody.velocity = Vector3(paddleSpeed * Time.deltaTime * Input.GetAxis("Horizontal"),paddleSpeed * Time.deltaTime * Input.GetAxis("Vertical"),0);
 
     //Launches the ball at the start of the game, as long as "codeBall" proves true first
     //Also moves the ball with the paddle prior to launching
     if (codeBall){
         codeBall.rigidbody.position = rigidbody.position + Vector3(0,.475,0);
             if (Input.GetButton("Launch")) {
             codeBall.rigidbody.isKinematic = false;
             codeBall.rigidbody.AddForce(300 * Input.GetAxis("Horizontal"),300,0);
             codeBall = null;
         }
     }
     
     //Moves the camera along with the paddle on the Y Axis
     Camera.mainCamera.transform.position = Vector3(0,transform.position.y,-10);
 }
 
 function OnCollisionEnter(paddleHitSomething : Collision) {
 
     //Alters the direction of a ball when it hits the paddle based on where it hit the paddle
     if (paddleHitSomething.gameObject.tag == "Ball"){
         for (contact in paddleHitSomething.contacts){
             if (contact.thisCollider == collider) {
                 var paddleHit = contact.point.x - transform.position.x;                
                 contact.otherCollider.rigidbody.AddForce( 100f * paddleHit, 0, 0);
             }
         }
     }
 }
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

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

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

Make Ray hit own collider 1 Answer

Collision position - Improving on the 'grounded' mechanic 2 Answers

Raycast doesnt detect object in front of rigidbody (player is stuck on wall) 1 Answer

Ragdoll collision precision 0 Answers

RAYCAST KNOWLEDGE HELP!!! 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