0

我在 Unity 中制作了汽车场景。我有一个Rigidbody(汽车)和它的childgameobject。我制作了一个脚本,在移动时childgameobject用滑块Rigidbody移动它。childgameobject滑块可以很好地移动,但是当我移动Rigidbody时,孩子GameObject保持在相同的位置并且不会随着Rigidbody. 这是我的代码:

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

public class ChildMove : MonoBehaviour
{
    public Slider MySliderx;
    public Slider MySlidery;
    public Slider MySliderz;
    public Transform Cam;
    public float temp;

    private float CamInitialY = 0f;
    private float CamInitialX = 0f;
    private float CamInitialZ = 0f;

    // Use this for initialization
    void Start () {
        CamInitialY = Cam.transform.position.y;
        CamInitialX = Cam.transform.position.x;
        CamInitialZ = Cam.transform.position.z;
    }

    // Update is called once per frame
    void Update () {
        Cam.transform.position = new Vector3 (Cam.position.x, CamInitialY + MySliderx.value , Cam.position.z);
        Cam.transform.position = new Vector3 (CamInitialX + MySlidery.value, Cam.position.y, Cam.position.z);
        Cam.transform.position = new Vector3 (Cam.position.x, Cam.position.y, CamInitialZ + MySliderz.value);    
    }
}
4

1 回答 1

0

通过将位置更改为本地位置来修复。

于 2020-06-04T08:33:19.487 回答