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




作者:艾孜爾江


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM