using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/*by Alexander*/

public class PositionChangerManager : MonoBehaviour
{
    public GameObject object2ChangePos = null;
    public GameObject objectParent = null;


    private Vector3 positionHolder = new Vector3(0f, 0f, 0f);



    void ChangePosition()
    {
        if (object2ChangePos != null && objectParent != null)
        {
            object2ChangePos.gameObject.transform.localPosition = new Vector3(
                                                                              objectParent.gameObject.transform.position.x,
                                                                              objectParent.gameObject.transform.position.y,
                                                                              objectParent.gameObject.transform.position.z);
        }
    }


    void ChangeToOriginalPosition()
    {
        //Change the object's position to the position stored
        object2ChangePos.gameObject.transform.localPosition = positionHolder;
    }



    void Start()
    {
        //Store the initial position first
        if (object2ChangePos != null)
        {
            positionHolder = new Vector3(
                                        object2ChangePos.gameObject.transform.position.x,
                                        object2ChangePos.gameObject.transform.position.y,
                                        object2ChangePos.gameObject.transform.position.z);
        }
    }


    void Update()
    {
        if (Input.GetKeyDown(KeyCode.C))
        {
            ChangePosition();
        }
        else if (Input.GetKeyDown(KeyCode.P))
        {
            ChangeToOriginalPosition();
        }
    }
}




作者:艾孜尔江

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2023-03-24
  • 2021-12-30
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-09
  • 2022-12-23
  • 2022-12-23
  • 2021-08-08
  • 2022-01-23
  • 2021-10-10
  • 2021-10-16
相关资源
相似解决方案