- Home /
ball breaks through walls
Hello. I am new to Unity and I am trying to create my first game. The basic idea is that this is a 2D game on a plane like space invaders. I have one enemy that moves back and forth on the x axis and it shoots down projectiles every few seconds (sphere game object). I want the projectiles to be able to bounce off all 4 walls of the game and keep moving in the game space. The problem is that the balls keep breaking through the lower boundary (All 4 walls have a rigidbody) . They seem to hesitate on collision for a second then break through. Is there anyway I can keep the balls constantly bouncing off the walls? (Like Pong if it had multiple balls and 4 walls instead of 2)
I included the code (Javascript) for my enemy's movement, and the ball itself (called enemyblast) Thank you for any help.
Enemy Movement:
var enemySpeed: int; var blast: Rigidbody; static var moveDir = Vector3.right;
//Launches Projectile on 2 second intervals
InvokeRepeating ("LaunchProjectile", 2, 2);
function LaunchProjectile () { var instance: Rigidbody = Instantiate(blast,transform.position, transform.rotation); instance.velocity = Random.insideUnitSphere *10; }
function Update () { var moveIt = enemySpeed * Time.deltaTime;
//make the enemy move side to side
if (transform.position.x >= 24)
{
moveDir = Vector3.left;
}
if (transform.position.x <= -24.0) { moveDir = Vector3.right; } transform.Translate(moveDir * moveIt);
}
Enemy Blast:
var blastSpeed: int;
//Makes the enemy fire downward
function FixedUpdate () { amtToMove = blastSpeed * Time.deltaTime;
transform.Translate (Vector3.down * amtToMove); }
Answer by torrente · Jul 04, 2012 at 12:38 AM
Welcome to the forums automan64!
I didn't run your code, but I think I see the problem with it. You are using transform.Translate to move the bullet. Doing this is a very strict way to move an object that is not based on any sort of collision. Translate ignores the rigidbody and any other component that might have an impact on the movement. I think this is what is causing you problems. Instead of this, take a look at this link. You need to use .AddForce(Vector3.down*amtToMove) to move rigidbodies around. A good practice is if you find yourself using .Translate to move an object around, take a moment to really make sure that the translation can happen blindly with no concern to anything else in the scene. Your other movements (the enemy ships) look like they are controlled by numeric values in code, the -24 and +24. Those are working well since they have a value hard coded in like that. But... once you fix the bullets, maybe see if you can implement the same sort of solution to their movement
The bullets are staying in play! Also, thanks for the tips on using .Translate. Thank you for taking the time to help me out!
Your answer
Follow this Question
Related Questions
unity bounce without physics 1 Answer
Velocity Movement & Physics Interactions by Rigidbody2D 0 Answers
Problems reading Rigidbody velocity magnitude 1 Answer
velocity bounce 0 Answers
C# Rotate velocity issue 1 Answer