UNITY C#-如何让2个对象始终具有相同的y值,但第二个-0.5单位

昨天我问了一个问题,该问题是关于如何始终使两个对象具有相同的y值。并且它起作用了,但是,我现在发布的是,我需要第二个多维数据集具有相同的y值BUT,且其第一个多维数据集的-Y值比第一个低-0.5个单位。我的原始多维数据集称为“播放器”,而脚本已附加到名为“ TestCube”的多维数据集。谢谢!


using UnityEngine;

using System.Collections;


public class testmovement : MonoBehaviour

{  

    Transform otherTransform;


    void Start()

    {

        // you can set a reference to the "parent" cube

        otherTransform = GameObject.Find("Player").transform;

    }


    void Update()

    {

        // here we force the position of the current object to have the same y as the parent

        transform.position = new Vector3(transform.position.x, otherTransform.position.y, transform.position.z);

    }

}


偶然的你
浏览 214回答 3
3回答

POPMUISE

您可以在功能参数列表中进行算术运算。所以写transform.position = new Vector3(transform.position.x, otherTransform.position.y -0.5f, transform.position.z);您可以实现恒定的Y偏移。

ITMISS

transform.position = transform.position + new Vector3(0, -0.5f, 0); //Ortransform.position = transform.position - new Vector3(0,  0.5f, 0);

呼唤远方

您也可以在“检查器”中执行此操作-使用层次结构设置关系Parent-Children(将一个对象放置在另一个对象上),然后将child相对于父位置变换组件IS,以便x:0,y:-0.5,z:0会给您预期的效果。
打开App,查看更多内容
随时随地看视频慕课网APP