Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Nightbadger · Jan 15, 2017 at 03:50 PM · collisionballbreakoutstoring

Bat and ball game - is there a way one can store the ball's last collision?

I'm controlling the bat with the mouse, the problem is that the mouse can overtake the ball when it is travelling on the x axis and collide with it multiple times, causing the ball to behave strangely and stick to the bat.

My solution was to store what the ball collided with last, and if the last collision was with the bat, the ball can't collide with the bat again until it has collided with something else (block, wall, another ball etc).

My script so far is in C#, and although I thought that the array function was the way to do it, it seems that is only available in javascript.

I'm sure I've done some debugging on a tutorial at some stage that stored the last object hit but I've done so many I can't remember which one it was.

Any help, even just pointing me towards the necessary function would be appreciated.

Thanks.

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
2

Answer by KoenigX3 · Jan 15, 2017 at 05:07 PM

 Collision lastCollision;
 
 void CheckLastCollision()
 {
     if(lastCollision)
     {
         if(lastCollision.gameObject.name == "Bat")
         {
             // Collided with the bat last time
         }
     }
 }
 
 void OnCollisionEnter(Collision coll)
 {
     lastCollision = coll;
 }

This creates a 'lastCollision' variable, which gets updated at every collision. You can check the last collision with the CheckLastCollision() function. If you want to check it on collision, be sure to write it at the beginning of the OnCollisionEnter void, so it will check the last collision first, then assign the new collision to it.

By the way, you can use

 Collision[] lastCollisions;

to create an array of collisions, if you want to check a collision history. Be sure to initialize the array in the Start() method with

 lastCollisions = new Collision[length];

and replace 'length' with a number. The update of this array is the following:

 for(int i = 0; i < lastCollisions.Length - 1; i++)
 {
     lastCollisions[i] == lastCollisions[i + 1];
 }
 lastCollisions[lastCollisions.Length - 1] = coll;

The function will first replace every collision in the array (starting from index 0) with the next collision in the array. Then the last one is replaced with the fresh collision ('coll').

However, the array has a fixed size. If you want a dynamic-sized container, you could use lists.

 using System.Collections.Generic;
 
 List<Collision> lastCollisions = new List<Collision>();
 
 lastCollisions.Add(coll);
 lastCollisions.RemoveAt(index);

The first line will create a list with the type of Collision, so it can hold only collisions. The second line can be used to add a collision to the list (the variable is named with 'coll', and it will be added to the end of the list). The third line can be used to remove a collision in the list with an index number.

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 Nightbadger · Jan 17, 2017 at 04:17 PM 0
Share

That's fantastic, thank you very much.

I think an "if" statement saying if the last object hit was the bat, to ignore collisions with the bat will do the trick.

Then when the ball hits another object the bat is back in play.

Thanks again.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Change direction of ball based on where it hits paddle -- 3d breakout game 0 Answers

Unrealistic bounce 2 Answers

Bounce when hitting wall 2 Answers

Problem with bat hitting ball in table tennis game 1 Answer

Detect when object in not colliding with collider. 3 Answers


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