I have made a car scene in Unity. I have one Rigidbody
(car) and its childgameobject
. I made a script to move that childgameobject
with sliders while Rigidbody
is moving. The childgameobject
moves fine with the slider but when my Rigidbody
moves, the child GameObject
remains in the same position and doesn't move with Rigidbody
. Here is my code:
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);
}
}
Fixed by changing position to localposition.