Unity实现物体位置变换

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();
        }
    }
}




作者:艾孜尔江

posted @ 2020-09-01 09:49  艾孜尔江  阅读(1910)  评论(0编辑  收藏  举报