GameObject Jittering
Hello, I'm posting a question because this is a specific case: I made a gameObject that both follows the player and sticks to the walls of the scene. Although most of the behavior works fine, when the player gets closer to the object, it increasingly jitters.
I think the reason is because of the way I coded the object but I can't find out what's the exact reason. The object's position is controlled by two scripts using Vector3.Lerp: one is to bound the objects to the walls of the scene and the other is to follow the player. since both acts in similar ways but in opposite direction, this is very likely the reason of this jittering
I'll post down below the code of the two scripts and a gif to give a broad idea
Code to follow the movements of the player
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerTracking : MonoBehaviour {
public GameObject player;
public float speed, DistLimite;
void LateUpdate ()
{
float step = speed * Time.deltaTime;
float dist1 = Vector3.Distance (player.transform.position, transform.position);
if (dist1 <= DistLimite)
{
transform.position = Vector3.Lerp (transform.position, player.transform.position, step);
}
}
}
And the code that places the object on the walls
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Attraction : MonoBehaviour {
public float speed;
public float Distance,ecart,verticaloffset, MaxRotation;
private GameObject MurProche, Surface;
private Renderer Ecran;
GameObject FindClosestWall()
{
GameObject[] gos;
gos = GameObject.FindGameObjectsWithTag("wall");
GameObject closest = null;
float distance = Mathf.Infinity;
Vector3 position = transform.position;
foreach (GameObject go in gos)
{
Vector3 diff = go.transform.position - position;
float curDistance = diff.sqrMagnitude;
if (curDistance < distance)
{
closest = go;
distance = curDistance;
}
}
return closest;
}
GameObject FindClosestEcran()
{
GameObject[] gos;
gos = GameObject.FindGameObjectsWithTag("Ecran");
GameObject closest = null;
float distance = Mathf.Infinity;
Vector3 position = transform.position;
foreach (GameObject go in gos)
{
Vector3 diff = go.transform.position - position;
float curDistance = diff.sqrMagnitude;
if (curDistance < distance)
{
closest = go;
distance = curDistance;
}
}
return closest;
}
void update ()
{
MurProche = FindClosestWall ();
Debug.Log ("Trouvé");
}
void LateUpdate ()
{
MurProche = FindClosestWall ();
Surface = FindClosestEcran ();
Ecran = Surface.GetComponent <Renderer> ();
Ecran.enabled = true;
Debug.Log ("Activé");
float step = speed * Time.deltaTime;
float dist2 = Vector3.Distance (MurProche.transform.position, transform.position);
Vector3 Direction = (1.1f * MurProche.transform.position - transform.position);
if (dist2 <= 25)
{
Ray LeftRay = new Ray (transform.position, Direction * Distance);
Debug.DrawRay (transform.position, Direction * Distance, Color.red);
RaycastHit LeftHitInfo;
if (Physics.Raycast (LeftRay, out LeftHitInfo, Mathf.Infinity))
{
float EcartMur = Vector3.Distance (LeftHitInfo.point, transform.position);
if (EcartMur > (ecart + 0.1f))
{
transform.position = Vector3.Lerp (transform.position, LeftHitInfo.point + LeftHitInfo.normal * ecart, step);
Debug.DrawRay (LeftHitInfo.point, LeftHitInfo.normal, Color.green);
Quaternion targetRotation = Quaternion.FromToRotation (Vector3.forward, LeftHitInfo.normal);
Quaternion FinalRotation = Quaternion.RotateTowards (transform.rotation, targetRotation, MaxRotation);
Vector3 temp = (transform.position);
temp.y = verticaloffset;
transform.position = Vector3.Lerp (transform.position, temp, Time.deltaTime * speed);
transform.rotation = Quaternion.Euler (0, FinalRotation.eulerAngles.y, 0);
}
}
}
}
}
Could anyone help me point out the issue? Final note, the player has a rigidbody, not the object, it has a trigger collider.
Your answer
Follow this Question
Related Questions
Lerp between two direction vector 0 Answers
hi everyone how can i move an object from a point to specific point(point b) by mouse click 0 Answers
How to get a velocity to use as a lerp time 0 Answers
How to position an object using the w axis from Vector4? 1 Answer
Vector3.Lerp not cooperating with me 0 Answers