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