# How to properly create a handle for setting moving platform's target position in Unity?

As the title suggests, I am trying to set up a handle for setting target position of a moving platform.

I have an empty GameObject(targetPos) and this is what I want to use for setting target position of the moving platform.

Here is the code I have for it so far but it does not seem to work correctly and I am not sure what is wrong.

The problem is, the empty gameobject's transform is not using correct position for moving platform to go to. Not that I put the empty gameobject as the child of the moving platform.

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

public class MovePlatform : MonoBehaviour
{
public Transform targetPos;
private Vector3 initialPosition, targetPosition, targetPosDebug;
public float speed = 3;
public bool loop;

void Start()
{
initialPosition = transform.position;
SetTargetPosition(targetPos.position);
}

void Update()
{
transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);

if (transform.position == targetPosition)
{
if (loop)
{
SwitchDirection();
}
}
}

public void SwitchDirection()
{
if (transform.position == initialPosition)
{
SetTargetPosition(targetPos.position);
}
else
{
targetPosition = initialPosition;
}
}

private void SetTargetPosition(Vector3 localPosition)
{
targetPosition = initialPosition + localPosition;
targetPosDebug = targetPosition;
}

void OnDrawGizmos()
{
if (Application.isPlaying)
{
Debug.DrawLine(initialPosition, targetPosDebug, Color.red);
}
else
{
Debug.DrawRay(transform.position, targetPos.position, Color.red);
Gizmos.DrawSphere(targetPos.position, 1.0f);
}

}
}

• Presumably you looked at the Handles API that's made for exactly this purpose? When you say your current code "does not seem to work correctly," can you explain how not? What do you want it to do? What does it do instead? How exactly does it differ from your desired behaviour? – DMGregory Mar 7 at 3:15
• No I do not want to make use of handles API. I want to attach an empty gameobject to my moving platform and wherever I drag my empty gameobject in the scene, that is where I want my moving platform to go to and then return back to it's initial position. – AaySquare Mar 7 at 12:54

You are treating the target transform’s absolute position as a local position offset. For example, if transform.position is (3,3,3) and targetPos is offset by (1,1,1) from that, as a child of this transform, its position is (4,4,4).

So when you call SetTargetPosirion, passing in (4,4,4), targetPosition is set to initialPosition + (mislabeled) localPosition, which is (3,3,3)+(4,4,4), or (7,7,7). What you really want is to either pass in targetPos.localPosition when calling SetTargetPosition, or do not call SetTargetPosition at all, instead just assign targetPosition = targetPos.position:

using UnityEngine;

public class MovePlatform : MonoBehaviour
{
public Transform targetPos;
private Vector3 initialPosition, targetPosition;
public float speed = 3;
public bool loop;

void Start()
{
initialPosition = transform.position;
targetPosition = targetPos.position;
}

void Update()
{
transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);

if (loop && transform.position = targetPosition)
{
SwitchDirection();
}
}
public void SwitchDirection()
{
if (transform.position == initialPosition)
{
targetPosition = targetPos.position;
} else {
targetPosition = initialPosition;
}
}
}

• You won't believe it. I just fixed my problem and was about to answer my own question using that method when I saw this a minute after. Yes you are right, that was the problem. Thanks! – AaySquare Mar 7 at 14:13