The problem is that the root character object have animator with idle animation playing.
If I don’t unchild the transform the transform will move to the target in strange path and also make the player jump and move.
Only when I unchild the transform it will move smooth to the target but then it’s not looking good because you see first time before it’s moving that the transform is not child and not on his start position.
I tried to put the code in LateUpdate but it didn’t help. Only unchild the transform move it smooth.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveToTarget : MonoBehaviour
{
public Transform target;
public float smoothTime = 0.3F;
private Vector3 velocity = Vector3.zero;
private void Start()
{
transform.parent = null;
}
void Update()
{
transform.position = Vector3.SmoothDamp(transform.position, target.position, ref velocity, smoothTime);
}
}
This is a screenshot showing where the transform with the script is child. The transform is child of the player hand.
And this cube should move to the Sphere.

When running the game a second before the cube start moving to the sphere the cube is unchild and it looks like this (paused the game to show it) :
Because I unchild the cube then the cube start moving not from the player hand.
And I want it to move from the player hand without unchild it but then it’s not moving directly to the sphere and make the player jump move.

This is working. I think almost perfect.
First I unchild the transform make a reference to it’s position then make it child back again.
Then in the LateUpdate I first lerp to the unchild position reference and from that position smoothdamp the transform to the target.
This way the unchild looks much more natural.
public Transform target;
public float smoothTime;
public float duration;
private Vector3 velocity = Vector3.zero;
private float timer = 0;
private Transform parent;
private Vector3 newPos;
private void Start()
{
transform.parent = null;
//Debug.Log("New pos : " + transform.position);
newPos = transform.position;
parent = GameObject.Find("rig_f_middle.03.R").transform;
transform.parent = parent;
}
void LateUpdate()
{
if (timer >= 1.0f)
{
transform.parent = null;
transform.position = Vector3.SmoothDamp(transform.position, target.position, ref velocity, smoothTime);
}
else
{
timer += Time.deltaTime;
float s = timer / duration;
this.transform.position = Vector3.Lerp(transform.position,
newPos, s);
}
}
Is this for the player throwing a ball or grenade or something from his hand?
Usually you would Instantiate() the object and supply the second argument as the Transform of the hand bone.
var grenade = Instantiate<GameObject>( GrenadePrefab, HandTransform);
Note: you are NOT giving it a position or rotation, unless you want to, but I would avoid that. Set the hand transform up so it doesn’t need extra “warts” of information.
This has two effects after it makes the grenade:
- parents the new object to the hand
- puts the new object AT the hand in space (also rotates it)
Then you would unparent it immediately (the next line of code) and let it go on its merry way.
grenade.transform.SetParent(null);
That way a) it will match where the hand was in the idle animation, and b) it can just go ballistic with another movement script after that.
1 Like