- Home /
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
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().
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.
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
Sigh, but now my ball get's "stopped" by the bricks ins$$anonymous$$d of bouncing off of them... What a bother...
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!
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);
}
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613093340im_/https://answers.unity.com/themes/thub/images/avi.jpg)