- Home /
Make an object a certain distance from me.
Hello so I have finally got some code working that enables an object to rotate and move towards another object when space bar is pressed. So an object will move to the player when space bar is pressed. Awesome, got that running! however it moves so freakin close to the player! I want it to move a little bit away from the player. Not to close, how can I do that. Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class lerpFinal : MonoBehaviour
{
//This is a new variable to store a Vector3 Position
private Vector3 newPosition;
private bool flagPos;
public Transform Player;
public Transform Target;
//Here we set the current position of the object using transform.position
private void Awake()
{
newPosition = transform.position;
}
//Here we are running positionChanging function in each framej
void Update()
{
PositionChanging();
transform.LookAt(Target);
}
void PositionChanging()
{
//Here is Where the positions are set
Vector3 positionA = new Vector3(0, 0, 0);
Vector3 positionB = Player.position;
//Inputing the following keys will allow us to Change the Position
if (Input.GetKeyDown(KeyCode.Space) && flagPos == false)
{
newPosition = positionA;
flagPos = true;
}
else if (Input.GetKeyDown(KeyCode.Space) && flagPos == true)
{
newPosition = positionB;
flagPos = false;
}
transform.position = Vector3.Lerp(transform.position, newPosition, Time.deltaTime);
}
}
Answer by rainChu · Aug 19, 2018 at 01:26 AM
I think I see what you're trying to do. Instead of Lerping towards the player's actual position, you should find a new position that's close to the player, along a line of sight.
Try this adding this to the bottom of PositionChanging()
:
void PositionChanging()
{
// ... Original Code as above ....
// Find the amount we have to move in total
var offset = transform.position - newPosition;
// Find the direction we have to move in
var direction = offset.normalized;
// Add 0.3 units to the newPosition, away from the player
newPosition += direction * 0.3f;
// ... Original Code as below ....
// Now you lerp
transform.position = Vector3.Lerp(transform.position, newPosition, Time.deltaTime);
}
Answer by oStaiko · Aug 19, 2018 at 02:44 AM
The exact solution here depends on how your game plays, as in is it on a 2D field, 3D, etc, but here's a generic solution you can modify to fit your needs:
public float dist = 3; // How far to stop (meters)
public float speed = 1; // How fast to follow (m/s)
public Transform player; // Reference to player
protected Vector3 targetPosition = Vector3.zero;
private bool isFollowing = false;
[SerializeField]
private bool debug = false;
void Update ()
{
if (isFollowing)
{
transform.LookAt (player);
float dist3 = speed*Time.deltaTime; // How far to move this frame
float dist2 = Vector3.Distance (targetPosition, transform.position); // Distance from target
if (dist3>dist2)
{
dist3 = dist2;
isFollowing = false; // At target, stop following. (Optional! Remove to follow forever)
if (debug) Debug.Log("Reached target!");
}
Vector3 direction = (targetPosition-transform.position).normalized;
transform.position += direction*dist3; // This moves the object each frame.
}
if (Input.GetKeyDown(KeyCode.Space) && !isfollowing)
{
Follow ();
}
else if (Input.GetKeyDown(KeyCode.Space) && isfollowing)
{
StopFollow ();
}
}
void Follow ()
{
if (debug) Debug.Log("Follow() called");
float dist2 = Vector3.Distance (player.position, transform.position); // Distance from player
if (dist2 >= dist ) // Don't do anything if its already in range
{
isFollowing = true;
float frac = 1 - (dist/dist2); // How far to travel, converts distances to a fraction
Vector3 targetPosition = Vector3.Lerp (transform.position, player.position, frac); // Gets position in straight line
if (debug) Debug.Log("New target:\n" + targetPosition.ToString());
}
else
{
if (debug) Debug.Log("Follow Cancled!\nAlready in range of target!");
StopFollow ();
}
}
void StopFollow ()
{
if (debug) Debug.Log("StopFollow() Called");
targetPosition = Vector3.zero // Not necessary, but can be nice to have for debugs
isFollowing = false;
}
Replace your script with this, it should do what you need, and a lot better than what you currently have. I didn't test it myself, so you just leave a comment if there's any errors you cant fix on your own.
As a side note, you really botched Vector3.Lerp(). Its a somewhat common mistake where it kinda works right, but you should really look in to the proper usage for it. Your usage makes the game play VERY differently and high and low FPS differences.
Good catch on Vector3.Lerp
. I agree that it has a huge impact on consistency when used in that way. In production games it shouldn't be used like that. But I feel it does a decent enough approximation of an ease out function for prototypes, so I don't $$anonymous$$d seeing it used like this.
Your answer
Follow this Question
Related Questions
Object not responding to OnTriggerEnter() 2 Answers
Slowly increase motor.force 1 Answer
Rotation question. 1 Answer
Player Prefs Dosen't work on android 0 Answers
Im having trouble using different scriptable objects in a script 0 Answers