0
\$\begingroup\$
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

Showing the hierarchy the transform that move Cube (1) with the script the target Sphere and the player rig hierarchy and the parent that in this case is the rig_f_middle.03.R object

\$\endgroup\$
5
  • \$\begingroup\$ 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? \$\endgroup\$
    – DMGregory
    Nov 25 at 21:33
  • \$\begingroup\$ @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); } \$\endgroup\$
    – Jhon RA
    Nov 25 at 22:16
  • \$\begingroup\$ 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. \$\endgroup\$
    – DMGregory
    Nov 25 at 22:17
  • \$\begingroup\$ @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. \$\endgroup\$
    – Jhon RA
    Nov 25 at 22:18
  • \$\begingroup\$ Presumably you considered moving to parent.position instead of parentPosition then? \$\endgroup\$
    – DMGregory
    Nov 25 at 22:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.