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 automan64 · Jul 03, 2012 at 08:42 PM · 2drigidbodyvelocityprojectilebounce

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); }

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
0
Best Answer

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

Comment
Add comment · Show 1 · 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 automan64 · Jul 04, 2012 at 04:29 AM 0
Share

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


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