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 RandomIntroStudent · Oct 05, 2017 at 06:51 PM · script.rigidbody2dfreeze

How do I freeze a rigidbody2d using script?

I'm learning in class, I would like to know how we would go about freezing the position of my rigidbody2d on respawn. I've already asked my teacher, he personally has never done it, so he doesn't know how.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by BlakeSchreurs · Oct 05, 2017 at 07:50 PM

There are a couple ways to do this. Try one or both to see which works best for your game.

If you change the RigidBody to be Kinematic, it will still retain physical properties, but not do any physics calculations for that body (Things can hit it, but it won't react, no reaction to gravity, etc).

         var item = Instantiate<GameObject>(prefab);
         var rb = item.GetComponent<Rigidbody2D>();
         rb.bodyType = RigidbodyType2D.Kinematic;

Another option is to turn on all constraints.

         var item = Instantiate<GameObject>(prefab);
         var rb = item.GetComponent<Rigidbody2D>();
         rb.constraints = RigidbodyConstraints2D.FreezeAll;
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 RandomIntroStudent · Oct 05, 2017 at 08:09 PM 0
Share

I tried that, I got two errors, here's my coding though, maybe it would help

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Level$$anonymous$$anager : $$anonymous$$onoBehaviour {

 public float waitToRespawn;
 public PlayerController thePlayer;


 // Use this for initialization
 void Start () {
     thePlayer = FindObjectOfType<PlayerController> ();

 }
 
 // Update is called once per frame
 void Update () {
     
 }

 public void Respawm()
 {
     StartCoroutine ("RespawnCo");
 }

 public IEnumerator RespawnCo()

 {
     //Sets Player = False
     thePlayer.gameObject.SetActive (false);
     //$$anonymous$$oves player to Spawn Position, waits, then sets true
     yield return new WaitForSeconds(waitToRespawn) ;
     thePlayer.transform.position = thePlayer.respawnPosition;
     thePlayer.gameObject.SetActive (true);


 }

}

avatar image BlakeSchreurs RandomIntroStudent · Oct 06, 2017 at 11:51 AM 1
Share

I refactored your RespawnCo a little, and added the RigidBody code. If this still creates errors, you'll need to show us what those errors are.

  public IEnumerator RespawnCo()
 
  {
     // Turn off the RigidBody
     var rb = thePlayer.gameObject.GetComponent<Rigidbody2D>();
     if (rb != null)  rb.bodyType = RigidbodyType2D.$$anonymous$$inematic;
     else Debug.Log("No RigidBody?");
 
     //Sets Player = False
     thePlayer.gameObject.SetActive (false);
 
     //$$anonymous$$oves player to Spawn Position, waits, then sets true
     yield return new WaitForSeconds(waitToRespawn);
     thePlayer.gameObject.SetActive (true);
     thePlayer.transform.position = thePlayer.respawnPosition;
     if (rb != null)  rb.bodyType = RigidbodyType2D.Dynamic;
  }
avatar image BlakeSchreurs BlakeSchreurs · Oct 06, 2017 at 01:27 PM 1
Share

Gah, sorry, I didn't see that thePlayer was of type "PlayerController ", made a $$anonymous$$or adjustment to code above.

Show more comments
avatar image
0

Answer by RandomIntroStudent · Oct 06, 2017 at 12:44 PM

I got the same error twice, it may be my fault, I'm just learning this, but, I got that "Type 'PlayerController' does not contain a definition for 'SetActive' and no extension method 'SetActive' of type "PlayerController' Could be found. Are you missing an assembly reference?"

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

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

73 People are following this question.

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

Related Questions

Rigidbody rotation: Use hinge joint motor or MoveRotation script? 0 Answers

Tag detection not working with rigidbody 0 Answers

Can we refer to two rigidbodies2d in a single script? 0 Answers

Objects moving despite everything being removed 0 Answers

I can't use Rigidbody2D in my scripts, I can't find rigidbody2d in my script 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