- Home /
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;
}
}
}
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.
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
Follow this Question
Related Questions
Performance Question on Kinematic Rigidbodies 1 Answer
Unexpected "Jumping" Behavior 0 Answers
Trouble Understanding Vector3.Angle() Result 2 Answers
RigidBodies and "Slipping" Prevention 0 Answers
Force to Velocity scaling? 2 Answers