Reputation: 11341
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 Text text;
public Text text1;
public float lookAtRotationSpeed;
public float moveSpeed;
private float minMoveSpeed = 0f;
private Vector3 originPos;
// Start is called before the first frame update
void Start()
{
originPos = targetToFollow.position;
}
// Update is called once per frame
void Update()
{
Vector3 lTargetDir = targetToFollow.position - transform.position;
lTargetDir.y = 0.0f;
transform.rotation = Quaternion.RotateTowards(transform.rotation,
Quaternion.LookRotation(lTargetDir), Time.time * lookAtRotationSpeed);
var distance = Vector3.Distance(transform.position, targetToFollow.position);
text.text = "Transform Distance From Target " + distance.ToString();
float ms = moveSpeed;
if (distance > 5f)
{
ms = moveSpeed + 0.5f;
}
else if (distance < 1.5f)
{
ms = Mathf.Max(minMoveSpeed, ms - 0.3f);
}
else
{
transform.position = Vector3.MoveTowards(transform.position, targetToFollow.position, Time.deltaTime * ms);
}
if(distance < 0.5f && originPos == targetToFollow.position)
{
ms = 0f;
}
transform.position = Vector3.MoveTowards(transform.position, targetToFollow.position, Time.deltaTime * ms);
originPos = targetToFollow.position;
}
}
When running the game the transform is moving to the target and slow down when getting close to the target then stop 1.5 distance from the target.
From this point when the target is moving around the transform is following the target and keeps the 1.5 distance.
The problem is when the target is standing and not moving and the transform is 1.5 from the target and then the target makes very small movements in this case the transform is moving to the target and after 2-3 times the transform reach and touch the target. but the transform should never get too close to the target the minimum distance should be 1.5
I want that the transform will start moving to the target when the target distance from the transform is above 1.5 to keep the transform following the target and if the distance is more then 5 then increase the transform speed to catch up the distance and get to 1.5 and then keep following again from 1.5 distance.
Upvotes: 0
Views: 117
Reputation: 20269
MoveTowards
will stop you exactly at the destination parameter. So instead of using the position of the target be your destination, find the position that's 1.5 units away from the target and use MoveTowards
with that.
From your description, you seem to only want to move when it is over 1.5 units away, so just don't move otherwise. This makes your code a lot easier to read.
void Update()
{
Vector3 lTargetDir = targetToFollow.position - transform.position;
lTargetDir.y = 0.0f;
transform.rotation = Quaternion.RotateTowards(transform.rotation,
Quaternion.LookRotation(lTargetDir), Time.time * lookAtRotationSpeed);
Vector3 fromHereToThere = targetToFollow.position - transform.position;
var distance = fromHereToThere.magnitude;
text.text = "Transform Distance From Target " + distance.ToString();
float ms = moveSpeed;
if (distance > 5f)
{
ms = moveSpeed + 0.5f;
}
Vector3 dest = targetToFollow.position - 1.5f * fromHereToThere.normalized;
if (distance > 1.5f)
{
transform.position = Vector3.MoveTowards(transform.position, dest,
Time.deltaTime * ms);
}
}
Upvotes: 2