Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Astadyl · May 18, 2017 at 11:06 AM · 3dvector3lerpjittering

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);
                 }
             }    
         }
     }
 }

alt text Could anyone help me point out the issue? Final note, the player has a rigidbody, not the object, it has a trigger collider.

jittering.gif (289.5 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

115 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges