我在 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);
}
}