• 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 boddole · Feb 05, 2014 at 10:32 AM · c#physicsrigidbody

RigidBody Script Conflict

Hello everyone, I've come across a point in my scripting, where I am trying to reset the position of a rigidbody, but another script seems to be interfering with it (and I don't see why).

Short description of my setup: I've got a bone with a animation that is moving meshes attached to a parent mesh (which is attached to the bone) with a rigidbody.

Description of my problem: My script to reset the animated bone works fine // And the script to reset the rigidbody works just as expected on its own. However, when both the bone script and rigidbody script are active at the same time, the rigidbody reset fails (the rigidbody velocity and angular rotation are stopped, but the rotation is frozen in place (is not set back to its original value). And I can't figure out why. Any ideas are appreciated.

Bone Script:

 using UnityEngine;
 using System.Collections;
 
 public class PlatformBoneController : MonoBehaviour {
 
     public Animator animator;
     public PlayerHealth playerHealth;
     public Vector3 startingLocation;
     public Vector3 startingRotation;
     public float animationSpeed;
 
 
     //starting of animation controlled by TriggerPlatformAnimation trigger.
 
     void Awake ()
     {
         animator = GetComponent<Animator>();
         playerHealth = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerHealth>();
         startingLocation = transform.position;
         startingRotation = transform.rotation.eulerAngles;
         animator.speed = animationSpeed;
 
     }
 
     void Update ()
     {
         ResetPlatform();
     }
 
 
 
     void ResetPlatform ()
     {
         if (playerHealth.isDead == true)
         {
             //Debug.Log("The player has died");
             animator.SetBool ("isStationary", true); //stop animation in place. 
             transform.position = startingLocation;
             transform.rotation = Quaternion.Euler (startingRotation); //should now be back in position, ready to be triggered again. 
 
 
         }
     }
 
 
 
 }

RigidbodyScript:

 using UnityEngine;
 using System.Collections;
 
 public class PlatformRigidBodyController : MonoBehaviour {
     //public Rigidbody platformRigidbody;
     public PlayerHealth playerHealth;
     public Vector3 startingLocation;
     public Quaternion startingRotation;
 
 
     void Awake ()
     {
         //Debug.Log ("this has been awakened");
         //platformRigidbody = GetComponent<Rigidbody>();
         playerHealth = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerHealth>();
         startingLocation = rigidbody.position;
         startingRotation = rigidbody.rotation;
     }
 
 
 
     void Update ()
     {
         ResetRigidBodyPosition ();
     }
 
 
 
     void ResetRigidBodyPosition ()
     {
         if (playerHealth.isDead == true)
         {
             Debug.Log ("resetting rigidbody");
             rigidbody.velocity = Vector3.zero;
             rigidbody.angularVelocity = Vector3.zero;
             rigidbody.position = startingLocation;
             rigidbody.rotation = startingRotation;
         }
     }
 
 
 }
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
1

Answer by Mortoc · Feb 05, 2014 at 03:26 PM

Right now your scripts are modifying transform and rigidbody. You want to modify one or the other per-gameobject, not both. Also, any modifications to rigidbody should be done during the Physics Update: FixedUpdate.

I'd recommend changing "void Update()" in both scripts to "void FixedUpdate()" and changing the body of ResetPlatform() to only access the rigidbody, not the transform.

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 boddole · Feb 06, 2014 at 02:55 AM 0
Share

Just for clarification: these scripts are on different gameobjects (they have a parent / child relationship). I did change the updates to fixedupdates as suggested, and I managed to get the rigidbody to cooperate by settings its position using localposition and localrotation ins$$anonymous$$d of global.

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

19 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

Related Questions

C# Need Help converting from Rigidbody to Character Controller 0 Answers

C# DragRigidbody ArgumentException: get_main can only be called from the main thread 2 Answers

C# Physics.LineCast Argument is Out Range 1 Answer

Problem with Rigidbody Parent/Child Relationship with "Teeter Totter" 0 Answers

Strange Rigidbody Behavior 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