为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
偶然的你 2021-04-02 14:15:46
昨天我问了一个问题,该问题是关于如何始终使两个对象具有相同的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);    }}
查看完整描述

3 回答

?
POPMUISE

TA贡献1765条经验 获得超5个赞

您可以在功能参数列表中进行算术运算。所以写

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

您可以实现恒定的Y偏移。


查看完整回答
反对 回复 2021-04-24
?
ITMISS

TA贡献1871条经验 获得超8个赞

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

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


查看完整回答
反对 回复 2021-04-24
?
呼唤远方

TA贡献1856条经验 获得超11个赞

您也可以在“检查器”中执行此操作-使用层次结构设置关系Parent-Children(将一个对象放置在另一个对象上),然后将child相对于父位置变换组件IS,以便x:0,y:-0.5,z:0会给您预期的效果。


查看完整回答
反对 回复 2021-04-24
  • 3 回答
  • 0 关注
  • 213 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号