When i change the parent of the object i want to reset its transform but it doesn’t work
here is my code:
using UnityEngine;
public class Reposition : MonoBehaviour
{
public Transform Myself;
public void Change()
{
Myself.transform.position = new Vector3(0, 0, 0);
Myself.transform.rotation = new Quaternion(0, 0, 0, 0);
}
}
If by resetting the transform you mean bringing position and rotation back to the initial global values then this is the code:
using UnityEngine;
public class Reposition : MonoBehaviour
{
public Transform Myself;
private Vector3 initialPosition;
private Quaternion initialRotation;
private void Awake()
{
initialPosition = Myself.position;
initialRotation = Myself.rotation;
}
public void Change()
{
Myself.transform.position = initialPosition;
Myself.transform.rotation = initialRotation;
}
}
If you want to reset the position and rotation to the same position and value relative to the new parent, then this is the right code instead:
using UnityEngine;
public class Reposition : MonoBehaviour
{
public Transform Myself;
private Vector3 initialLocalPosition;
private Quaternion initialLocalRotation;
private void Awake()
{
initialLocalPosition = Myself.localPosition;
initialLocalRotation = Myself.localRotation;
}
public void Change()
{
Myself.transform.localPosition = initialLocalPosition;
Myself.transform.localRotation = initialLocalRotation;
}
}