Home > front end >  How to reset a particualr object to its original rotation?
How to reset a particualr object to its original rotation?

Time:10-27

I'm trying to set objects with the tag "chair" to their original rotation once they are picked up.

Here is what I tried to do:

 if (currentlyPickedUpObject != null)
        {

            if (currentlyPickedUpObject.tag == "chair")
            {
                lookRot = Quaternion.LookRotation(mainCamera.transform.position - pickupRB.position);

                lookRot = Quaternion.Slerp(mainCamera.transform.rotation, lookRot, rotationSpeed * Time.fixedDeltaTime);

                //pickupRB.MoveRotation(lookRot);

                Vector3 angles = new Vector3(-90, 0, pickupRB.transform.eulerAngles.z);
                pickupRB.transform.rotation = Quaternion.Euler(angles);
            }
            reticle.enabled = false;
        }

but this doesn't work well for all the chairs in the scene, some are working and some of them get roatated in a different way.

I want all of the object with this tag to get their original rotation when the player picks them up. some secnes have one chair and others have two or more. so If there is a way to do the rotation automatically without me seting the number of chairs and their orginal vlaues, it would be super useful. please help

Update: according to derHugo's answer, I added this script and attached it to all chairs on my scene. Here is the script:

public class Chair: MonoBehaviour 
{
    Interactions interaction;
    private Quaternion originalRotation;
    private GameObject game;  
    void Start()
    {
        originalRotation = transform.rotation;
        GameObject playerinter = GameObject.Find("ThePlayer");
        interaction = playerinter.GetComponent<Interactions>();
    }

    private void Update()
    {
        if (interaction.currentlyPickedUpObject == game ) 
        {
            ResetRotation();
        }
    }
    // Update is called once per frame
    public void ResetRotation()
    {
        transform.rotation = originalRotation;
    }
}

Now, the chairs are always set to their original rotation even when they are thrown away they get back up to the original rotation! This should not be happening, it should only reset the rotation when they are picked up. (if they are thrown away or get hit by another object they should fall according to the right physics).

CodePudding user response:

You could simply store that original rotation and assign it back later

You could e.g. have a component on the chairs like

public class RotationResetter : MonoBehaviour
{
    private Quaternion originalRotation;

    private void Start()
    {
        originalRotation = transform.rotation;
    }

    public void ResetRotation()
    {
        transform.rotation = originalRotation;
    }
}
  • Related