• 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
Question by Jordi · Oct 16, 2010 at 12:01 PM ·

Resetting a position

Ive made a game where there are two states of play: Play (where objects are affected by gravity and are not kinematic) and Stop (where they are not, and a player can move them around).

By all means I can t$$anonymous$$nk of, t$$anonymous$$s following code should work, but it doesn;t.

void LateUpdate () {

 if (playScript.gamePlays == false) { //if game is not playing
     if (playedLastFrame == true) { // but if game did play last frame
         transform.position = new Vector3(initialTransform.position.x, initialTransform.position.y, initialTransform.position.z);
         transform.rotation = initialTransform.rotation;
         transform.localScale = initialTransform.localScale; 
     }
     playedLastFrame = false;
     rigidbody.isKinematic = true;
     rigidbody.useGravity = false;
 } else {   //if game currently plays
     if (playedLastFrame == false) {   //if game last frame didn't play (so button was pressed)
         initialTransform.position = t$$anonymous$$s.gameObject.transform.position;
         initialTransform.rotation = t$$anonymous$$s.gameObject.transform.rotation;
     }   
     playedLastFrame = true;
     rigidbody.isKinematic = false;
     rigidbody.useGravity = true;

 }




}

}

In the playScript, there is a button that when pressed on (in the ONGUI function) sets gamePlays to true or false. At that point it should check wether or not the game was playing in the last frame. If it was playing in the last frame and isn't now,it should set the transform of the item to its initialTransform. But it doesn't. Why?

Comment

People who like this

0 Show 0
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

Answer by skovacs1 · Oct 18, 2010 at 08:30 PM

You don't really say what it is doing incorrectly, but if I had to guess, I'd suspect your problem is your initialTransform. Since you can't access the constructor for Transform, I imagine you are assigning it some other Transform. Transform is a reference type, so if initialTransform == transform, then transform.position = initialTransform.position is meaningless. You'll need to keep your position, rotation and scale as individual variables.

There's a bunch not included in the question that could be wrong with your variable declarations and their initial values as well.

You don't need t$$anonymous$$s.gameObject. since transform is a publicly accessible member of MonoBehaviour w$$anonymous$$ch your class should inherit if you want LateUpdate to even be called.

If you were to simplify your variables and functions into somet$$anonymous$$ng more legible, it would be much easier to follow what is happening and what is wrong.

Keep it simple

Why are you calling all t$$anonymous$$s every frame and having to check your button state the last frame when you could just have your button call the function on the object(s) when it is pushed?

TogglePhysics.cs

using UnityEngine;

public class TogglePhysics : MonoBehaviour { public Transform target;

 Vector3 pausePosition;
 Vector3 pauseScale;
 Quaternion pauseRotation;
 string text;

 void Start() {
    if(target) {
        pausePosition = target.position;
        pauseScale = target.localScale;
        pauseRotation = target.rotation;
    }
    ButtonOn();
 }

 void OnGUI() {
     if(GUI.Button(new Rect(10, 10, 150, 100), text)) {
         if(text == "ON") ButtonOff();
         else ButtonOn();
     }
 }

 void ButtonOff() {
     text = "OFF";
     if(target) {
         if(target.rigidbody) {
             target.rigidbody.isKinematic = true;
             target.rigidbody.useGravity = false;
         }
         pausePosition = target.position;
         pauseScale = target.localScale;
         pausesRotation = target.rotation;
     }
 }

 void ButtonOn() {
     text = "ON";
     if(target) {
         if(target.rigidbody) {
             target.rigidbody.isKinematic = false;
             target.rigidbody.useGravity = true;
         }
         target.position = pausePosition;
         target.localScale = pauseScale;
         target.rotation = pauseRotation;
     }
 }

}

From the object perspective

To do it the way you have it would work like:

ToggleButton.cs

using UnityEngine;

public class ToggleButton: MonoBehaviour { string text; public static bool ButtonOn;

 void Start() {
    ButtonOn();
 }

 void OnGUI() {
     if(GUI.Button(new Rect(10, 10, 150, 100), text)) {
         if(ButtonOn) ButtonOff();
         else ButtonOn();
     }
 }

 void ButtonOff() {
     text = "OFF";
     ButtonOn = false;
 }

 void ButtonOn() {
     text = "ON";
     ButtonOn = true;
 }

}

TogglePhysics.cs

using UnityEngine;

[RequireComponent (typeof (Rigidbody))] public class TogglePhysics : MonoBehaviour { Vector3 pausePosition; Vector3 pauseScale; Quaternion pauseRotation; bool currentState;

 void Start() { //Initialize
    pausePosition = transform.position;
    pauseScale = transform.localScale;
    pauseRotation = transform.rotation;
    TurnOn();
 }

 void LateUpdate() {
     if(currentState == ToggleButton.ButtonOn) return; //we're good

     if(ToggleButton.ButtonOn) TurnOn();
     else TurnOff();
 }

 void TurnOn() {
     rigidbody.isKinematic = false;
     rigidbody.useGravity = true;
     transform.position = pausePosition;
     transform.localScale = pauseScale;
     transform.rotation = pauseRotation;
     currentState = true;
 }

 void TurnOff() {
     rigidbody.isKinematic = true;
     rigidbody.useGravity = false;
     pausePosition = transform.position;
     pauseScale = transform.localScale;
     pausesRotation = transform.rotation;
     currentState = false;
 }

}

If you knew not$$anonymous$$ng else was changing your booleans, you could even simplify boolean assignments to be rigidbody.isKinematic = !rigidbody.isKinematic; and the like.

Comment
Jordi
marks.du

People who like this

2 Show 0 · 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

Answer by Jordi · Oct 19, 2010 at 07:27 AM

I see what you mean. I'll wrestle through the code, try it out t$$anonymous$$s afternoon and update you on the progress. Thanks in advance ^^

Comment
marks.du

People who like this

1 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 Bravini · Oct 19, 2010 at 04:18 PM 0
Share

please use comments instead of answers for replies :]

avatar image

Answer by Bravini · Oct 18, 2010 at 08:51 PM

Skovacs code is flawless except that if I got the question right, Jordi wants to return all the transforms to initial position when paused, so the snippet

void TurnOff() {

   rigidbody.isKinematic = true;
   rigidbody.useGravity = false;
   pausePosition = transform.position;
   pauseScale = transform.localScale;
   pausesRotation = transform.rotation;
   currentState = false;
}
 <p>should go as</p>

void TurnOff() {

   rigidbody.isKinematic = true;
   rigidbody.useGravity = false;
   currentState = false;
}

having the pause vars always setted to the starting position.

Comment

People who like this

0 Show 0 · 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

Answer by Jordi · Oct 21, 2010 at 06:53 PM

It works for just one snag: it didn't recognise the difference between bool ButtonOff and function ButtonOff(), so just had to rename those. Awesome! Now off to making mouseOver and mouseExit GUI elements ^^

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.


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