Reputation: 53
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;
}
}
Upvotes: 0
Views: 1944
Reputation: 1498
The child
and parent
variables are already transform
s, 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