不与父对象一起移动的子项

时间:2017-04-29 08:36:59

标签: c# unity3d

我有一个玩家对象执行动画进入汽车,完成这个动画后,我正在使用animationEvent调用一个函数SettoParent(),它可以很好地工作。

必须将玩家对象设置为Car Object的子对象,该对象运行良好。

但是当我驾驶汽车时,玩家不会与汽车一起移动。

函数SetToParent()附加到播放器对象

我使用了以下代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SettingParent : MonoBehaviour {

    public Transform parent;
    public Transform child;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }

    public void SetToParent(){

        child.transform.parent = parent.transform;
    }
}

enter image description here

1 个答案:

答案 0 :(得分:1)

childparent变量已经是transform s,因此您可以按照以下方式使用它们 -

public void SetToParent(){
    child.parent = parent;
}

在检查员中,将parent设置为Car,将child设置为Player/FullPlayerObject

相关问题