- Home /
Reset Joints to initial state
Description: Hello, I made a boxing bag using Rigidbodies and Character Joints. You can move the bag and hit it with other objects to move it like a ragdoll.
The problem is that I want when I press SPACE to reset it's position and rotation back to it's initial state.
I tried to do this using the script below. The problem is that the joints don't return to their initial state and the bag keeps moving.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ReturnToStartPos : MonoBehaviour
{
Vector3 InitialPos;
Vector3 LocalPos;
Quaternion LocalRot;
Quaternion InitialRot;
private Rigidbody rb;
void Start(){
InitialPos = transform.position;
InitialRot = transform.rotation;
LocalPos = transform.localPosition;
LocalRot = transform.localRotation;
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update(){
if (Input.GetKeyDown("space"))
{
transform.rotation = InitialRot;
transform.position = InitialPos;
transform.localPosition = LocalPos;
//Quaternion.Euler(LocalRot);
transform.localEulerAngles = new Vector3 (LocalRot.y, LocalRot.x, LocalRot.z);
rb.velocity = Vector3.zero;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
How do I make a rigidbody 3D wall jump? 1 Answer
Throwing a Spear 1 Answer
Ragdoll problem with animations 0 Answers