0
\$\begingroup\$
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;

public class Follow : MonoBehaviour
{
    public Transform targetToFollow;
    public Transform missionTarget;
    public Text textDistance;
    public Text textSpeed;
    public float lookAtRotationSpeed;
    public float moveSpeed;
    public float followRadius = 1.5f;
    public float fastRadius = 5f;
    public float speedBoost = 0.5f;

    void Start()
    {
        
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        Vector3 lTargetDir = targetToFollow.position - transform.position;
        lTargetDir.y = 0.0f;
        transform.rotation = Quaternion.RotateTowards(transform.rotation,
            Quaternion.LookRotation(lTargetDir), Time.time * lookAtRotationSpeed);

        float ms = moveSpeed;
        var distance = Vector3.Distance(transform.position, targetToFollow.position);
        // Compute a position no further than followRadius away from our target.
        Vector3 fromTarget = Vector3.ClampMagnitude(-lTargetDir.normalized, followRadius);
        Vector3 stopPoint = targetToFollow.position + fromTarget;

        // Compute a speed that's faster when far away and slower when close.
        float speedBlend = Mathf.Clamp01((distance - followRadius) / (fastRadius - followRadius));

        ms = moveSpeed + speedBlend * speedBoost;

        // Move as far as we can at our speed ms to reach the stopPoint, without overshooting.
        transform.position = Vector3.MoveTowards(transform.position, stopPoint, Time.deltaTime * ms);
    }

    private void Mission()
    {

    }
}

I mean the transform of the gameObject the script is on. I want to keep the y position up all the way or at least make it finish moving on the ground or above the ground depending on my settings.

Now the transform the script is attached to the name is NAVI. NAVI is by default position Y set to 5.94 :

NAVI position on Y is 5.94

Then the game start NAVI is moving to the target the kid_from_space in this case. Kid_from_space position is at 0,0,0 when the NAVI getting close to the target NAVI position Y is 0 and the result is :

NAVI position on Y is 0

Therefore the NAVI is not on the ground and I also can't change it's Y position up or down. The NAVI is stuck at the Y 0 position. I want to be able to change the Y position up/down, for example if before the game is running I'm setting the Y position of NAVI to 5.94 or 6 or 21 I want the NAVI to stay at this position on the Y and not move down to 0.

\$\endgroup\$
4
  • \$\begingroup\$ I don't understand this question. You are asking about "the" transform, but there are at least 3 transforms here: the transform of the gameObject this script is on, the targetToFollow and the missionTarget (although the latter seems unused). Which transform do you change, what do you expect that to do and what happens instead? Also, what do you mean with "height"? A transform has no "height". It only has a position and a scale. And which axis is "height" in your game? Usually "up" is y, but when your game doesn't use gravity, then "up" can be whatever you want. \$\endgroup\$ – Philipp Dec 22 '20 at 12:15
  • \$\begingroup\$ It sounds like they're trying to change the y-coordinate elevation of an object. They have a character whose transform origin is in their center. Then they have this script, that positions that origin on the same y plane as their target. I think they want to lift the character up to sit above the target's y plane (and didn't try adding their desired elevation to stopPoint.y for some reason?) \$\endgroup\$ – DMGregory Dec 22 '20 at 12:55
  • \$\begingroup\$ @Philipp You right I mean the transform of the gameObject this script is on. Height I mean the y position up. I will edit my question with a screenshot of what is happening and what I want to do. \$\endgroup\$ – Shamen Raze Dec 22 '20 at 17:31
  • \$\begingroup\$ @DMGregory You right too. I edited my question with two screenshots and explain of what I mean and what I'm trying to do. \$\endgroup\$ – Shamen Raze Dec 22 '20 at 17:40
0
\$\begingroup\$

The culprit appears to be this line:

lTargetDir.y = 0.0f;

This makes this object head for the direction of the floor at the position of the object its following. When you want this object to retain its y-coordinate, then it should be sufficient to add back the transform.position.y of this object which you just subtracted on the line above:

lTargetDir.y += transform.position.y;
\$\endgroup\$
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.