[Solved] Find the point at a certain distance in a particular direction from another point?

I have an object with a Vector3 called startPosition, a Vector3 called targetPosition, a Vector3 called direction, and a float called maxDistance.

The object begins at startPosition (let’s say 0,0,0) and continually moves in that direction (let’s say forward, 0,0,-1) on a downward angle.
I’m wondering how to set targetPosition to the point at maxDistance in direction from startPosition.

I am looking for this because I would like to check when the object has reached targetPosition by comparing its transform.position with targetPosition, both rounded to two decimal places. If they are the same, I would like to make the object appear back at startPosition. My only difficulty is the bolded sentence above. I am open to easier ways of achieving this too. I managed to do this using a much more simple method on a flat plane, but I’m having much more difficulty when the object is moving on an angle.

Thank you!

Hey all,

I’ve managed to find what I was looking for. It was a matter of calculating the direction relative to the rotation of the object. Here’s the final code, and it does exactly what I was looking for:

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

public class menuSlide : MonoBehaviour {

    private Vector3 direction = new Vector3(0, 0, -1);
    private Vector3 startPosition;
    private Vector3 targetPosition;
    private Vector3 tDirection;
    public float speed = 2f;
    public float maxDistance = 18f;

    // Use this for initialization
    void Start () {

        startPosition = new Vector3(transform.position.x, transform.position.y, transform.position.z);
        tDirection = transform.rotation * direction;
        targetPosition = startPosition + tDirection * maxDistance;

    }
   
    // Update is called once per frame
    void Update () {

        if (transform.position.z > targetPosition.z)
        {
            transform.Translate(direction * speed * .01f);
            print("name: " + this.gameObject.name + ". transform.position: " + transform.position + ". targetPosition: " + targetPosition + ". startPosition: " + startPosition);
        }
        else
        {
            transform.position = startPosition;
            print("RESET");
        }
    }
}
3 Likes