Gagan Ahuja
Gagan Ahuja

Reputation: 53

Child not moving with Parent Object

I have a Player Object which performs an animation for entering the car and after completion of this animation, I'm calling a function SettoParent() using animationEvent, which works perfectly.

The Player Object has to be set as a child to the Car Object, which is working perfectly.

But when I drive the car, the player does not move together with the car.

The function SetToParent() is attached to the Player Object

I have used the following code:

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

Upvotes: 0

Views: 1944

Answers (1)

Mukesh Saini
Mukesh Saini

Reputation: 1498

The child and parent variables are already transforms, so you can use them as follows -

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

In inspector set the parent to Car and child to Player/FullPlayerObject.

Upvotes: 1

Related Questions