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 /
  • Help Room /
avatar image
1
Question by BramNH · Oct 16, 2015 at 12:21 PM · sprite2d gamecollision2dfreezepositionfreezerotation

How to freeze an object when collision?

Hello,

I have a 2D catch game, where you have to catch falling objects. Now I have a life and score script, when I don't catch my falling object, it collisions with the ground and changes the sprite. So I have bottles falling from the air, and when they fall on the ground, it changes the sprite to a broken bottle.

In this script, I have also set, if the (other.gameObject.GetComponent().sprite == broken) then use the function lifeSystem.TakeLife(); which will take a life off.

But my problem is, the bottle can rotate, so when the sprite has changed, it could rotate on the ground, so the game is seeing it as more collisions, so more lifes are taking off. Now what I want is when my bottle hits the ground and changes the sprite, I want it to stand still, so it can't rotate or move.

What also an option is: When my bottle hits the ground, it will paint a sprite on the position of where it fell. But I have no idea how to do this.

Bram

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

4 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by meat5000 · Oct 16, 2015 at 02:32 PM

If it has a rigidbody, make that rb Kinematic. If you dont want to make it kinematic manipulate the rigidbody 'Constraints'.

Comment
Add comment · Show 7 · 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 StianC · Oct 16, 2015 at 03:32 PM 0
Share

This will also work

avatar image BramNH · Oct 16, 2015 at 03:49 PM 0
Share

Yes I am using a rigidbody, but I can't turn on $$anonymous$$inematic, because then it won't fall down. And if I use the Contraints, I can't catch the bottle realistic, like if the bottle hits the edge of the catch object, it doesn't rotate.

avatar image meat5000 ♦ BramNH · Oct 16, 2015 at 05:57 PM 0
Share

No No, $$anonymous$$ake it $$anonymous$$inematic On Impact. This will freeze it in place after it has fallen. You can do this step in the OnCollisionEnter function.

If you want it to make a kinematic object move with another one, make the kinematic object a child of the other.

avatar image BramNH meat5000 ♦ · Oct 17, 2015 at 09:13 AM 0
Share

Okay I did this:

I made a public Rigidbody2D rb; (and put my bottles rigidbody into)

in the void start:

rb= GetComponent ();

and in the function of the impact:

rb.is$$anonymous$$inematic = true;

But when my bottle hits the ground, it isn't kinematic.

Show more comments
avatar image
0

Answer by StianC · Oct 16, 2015 at 02:29 PM

If you are using a rigidbody on your falling objects, just turn it off on the collision. gameObject.GetComponent<RigidBody2D>().enabled = false;

If not, turn of whatever component is causing it to move down the screen.

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 BramNH · Oct 16, 2015 at 03:44 PM 0
Share

Okay, that's sounds like the solution.

Here is my script:

 public Sprite $$anonymous$$rolsch;
 public Sprite $$anonymous$$apot;

 private Life$$anonymous$$anager lifeSystem;

 void Start () {

     lifeSystem = FindObjectOfType<Life$$anonymous$$anager> ();
 }

 void OnCollisionEnter2D (Collision2D other) {

     if (other.gameObject.GetComponent<SpriteRenderer>().sprite == $$anonymous$$rolsch)
     {
         other.gameObject.GetComponent<SpriteRenderer>().sprite = $$anonymous$$apot;
     }
     if (other.gameObject.GetComponent<SpriteRenderer>().sprite == $$anonymous$$apot)
     {
         lifeSystem.TakeLife();
     }
 }

How do I have to write this in the script?

avatar image
0

Answer by BramNH · Oct 18, 2015 at 04:32 PM

Right now, when my bottle hits the ground and changes the sprite, i have set the following:

 public Sprite Krolsch;
 public Sprite Kapot;
 public Rigidbody2D rigidbody;

 private LifeManager lifeSystem;

 void Start () {

     lifeSystem = FindObjectOfType<LifeManager> ();
 }

 void OnCollisionEnter2D (Collision2D other) {

     if (other.gameObject.GetComponent<SpriteRenderer>().sprite == Krolsch)
     {
         other.gameObject.GetComponent<SpriteRenderer>().sprite = Kapot;
     }
     if (other.gameObject.GetComponent<SpriteRenderer>().sprite == Kapot)
     {
         lifeSystem.TakeLife();
         rigidbody.isKinematic = true;
     }
 }

So the sprite is changed, the rigidbody of it is kinematic.

But I am using a spawner, so when the next bottle spawns, it doesn't fall anymore, how to fix this??

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
avatar image
0

Answer by constantlime830 · Jul 29, 2020 at 01:45 PM

@BramNH how about make a private bool called is smashed and set it to false, then go to the part of your script that changes it to kinematic and put is smashed there and set it to true then in an update function type

if(IsSmashed == true) { rb.isKinematic = false }

then the next one should fall

p.s i have no idea if this works or not, but i think it should :)

Comment
Add comment · Show 2 · 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 constantlime830 · Jul 29, 2020 at 01:45 PM 0
Share

ive only just noticed this was in 2015 lol

avatar image BramNH constantlime830 · Jul 29, 2020 at 03:37 PM 0
Share

I was reading through my emails and saw someone had commented on me on the unity forum. I couldn't even remember I had used it :P This project has been left for dead a long time ago.

Still thanks for the answer! Even though I'm not gonna implement it :)

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Sprites pixel colliding with 2d collider 0 Answers

Sprite Orbiting Sprite with Mouse 0 Answers

Freeze players position and rotation for a time? 0 Answers

How do I animate a 2D weapon? 0 Answers

shader graph sprite shader flickers in build when moving camera 0 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