# How can I move the object back to the parent position? I want the transform to move to the target with smoothdamp and then back to the original pos

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveToTarget : MonoBehaviour
{
public Transform target;
public Transform parent;
public float smoothTime;

private bool isMoving = false;
private bool moveBack = false;
private Vector3 velocity = Vector3.zero;
private Vector3 newPosition;
private Vector3 parentPosition;

private void Awake()
{
transform.position = parent.position;
transform.rotation = parent.rotation;

parentPosition = parent.position;
}

void LateUpdate()
{
if (Input.GetKeyDown(KeyCode.M))
{
isMoving = true;
}

if (isMoving)
{
newPosition = Vector3.SmoothDamp(transform.position, target.position, ref velocity, smoothTime);

if(newPosition == transform.position)
{
Debug.Log("Reach target : " +
newPosition + " transform position : " + transform.position);

moveBack = true;
}

if(moveBack)
{
newPosition = Vector3.SmoothDamp(transform.position, parentPosition, ref velocity, smoothTime);
}

transform.position = newPosition;
}
else
{
transform.position = parent.position;
transform.rotation = parent.rotation;
}
}
}


First when I press the M key the transform is moving from the parent to the target.

The problem is somewhere at this part :

if(moveBack)
{
newPosition = Vector3.SmoothDamp(transform.position, parentPosition, ref velocity, smoothTime);
}


For some reason the transform is stopping in the middle of the way back to the parent. The transform is never reaching the parent position.

What I'm trying to archive.

The transform this script is attached to is a Cube (1) because the player have animator I decided not to child the Cube (1) to the player hand but to put the Cube in the main hierarchy level and in the script to set the cube position and rotation to the player hand so it will look like a child.

This part is working fine.

Then I want to make that when I click the M key the transform the cube will move with smoothdamp to the target(sphere) and when reaching to the target move back also with smooth damp. back to the original position but because the parent variable is all the time changing position because the player animator then the parentPosition reference in the Start() is wrong.

This is a screenshot showing the transform the Cube (1) the Sphere and the player rig and the object child that the Cube (1) is position and rotation with it.

The parent is the object rig_f_middle.03.R

• I notice you're not disabling the movement toward the target when moving back - is that intentional? What's your expected outcome when trying to use the same velocity variable to simultaneously move toward the target and toward the parent? Nov 25 '21 at 21:33
• @DMGregory right. so I disable now the movement to the target with this if (moveBack == false) { newPosition = Vector3.SmoothDamp(transform.position, target.position, ref velocity, smoothTime); } Nov 25 '21 at 22:16
• If you'd like to clarify your question, click the Edit button. Be sure to describe how the behaviour of the new code differs from what you want. Nov 25 '21 at 22:17
• @DMGregory but there some more problems. now the transform is moving back but the parent position is not the position I reference in the Start() because the player have animation and the parent position is changing all the time. so the position in the Start is not the position when moving back to parent. so parentPosition will not be the same as in the Start. Nov 25 '21 at 22:18
• Presumably you considered moving to parent.position instead of parentPosition then? Nov 25 '21 at 22:26