Object stops lerping when target is far away
I have a child gameobject that the parent player shoots out. The child follows where the player clicked, and then when its reached its destination it moves back towards the player. It usually works fine, but whenever the player is too far away, the object suddenly stops lerping towards it. Whenever I get closer it starts working again for some reason. Does anyone know how to fix this. Heres the child code-
public Transform ParentTransform;
public SpriteRenderer sr;
private Vector3 MousePosition;
private Vector2 MouseDirection;
private Vector2 RayEnd;
private bool Grappling;
void Start()
{
sr.enabled = false;
}
void Update()
{
if(Input.GetKeyDown(KeyCode.Mouse0) && transform.position == ParentTransform.position)
{
sr.enabled = true;
Grappling = true;
MousePosition = (Vector3)Camera.main.ScreenToWorldPoint(Input.mousePosition);
MousePosition.z = 0;
MouseDirection = MousePosition - Camera.main.transform.position;
RaycastHit2D Hook = Physics2D.Raycast(transform.position, MouseDirection, 1);
RayEnd = new Vector2(transform.position.x + MouseDirection.x * 1, transform.position.y + MouseDirection.y * 1);
transform.position = Vector3.MoveTowards(transform.position, RayEnd, 6 * Time.deltaTime);
Debug.Log(MouseDirection);
}
if(Grappling)
{
transform.position = Vector3.MoveTowards(transform.position, RayEnd, 6 * Time.deltaTime);
}
if(!Grappling)
{
transform.position = Vector3.MoveTowards(transform.position, ParentTransform.position, 10 * Time.deltaTime);
}
if(transform.position.x == RayEnd.x && transform.position.y == RayEnd.y)
{
Grappling = false;
}
if(transform.position == ParentTransform.position)
{
sr.enabled = false;
}
}
Your answer
Follow this Question
Related Questions
Looking for the optimal smooth incremental movement method. 0 Answers
Move Gameobject towards/away from position based on the proximity of 2 other objects 0 Answers
Objects follow player and mimics player movement (C#) 0 Answers
transform.position is not updating the object's position 1 Answer
Moving a GameObject that was instantiated,Problem moving an instantiated Object. 0 Answers