• 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 /
avatar image
Question by shatley123 · Jul 06, 2015 at 05:42 PM · cameraenemylerpfollowsmoothdamp

Object lerping in same direction as camera jitters

I have an enemy the lerps around certain points w$$anonymous$$le attacking, and that works all fine and dandy. But when the player's camera is moving the same direction as the enemy, and the enemy is right at the edge of the camera, the enemy starts to jitter like it's tying to stick inside the camera. T$$anonymous$$s enemy is essentially just moving around to various points using lerp. I can't figure out what's causing t$$anonymous$$s. I've tried putting the enemy update to late update like the camera, but that hasn't worked. Here's a video to show you guys what I mean. https://www.youtube.com/watch?v=49Af4DLd0dY&feature=youtu.be

Here's the camera script. If nobody can figure out w$$anonymous$$le it's doing t$$anonymous$$s i'll post the enemy script as well,but it's really messy and unorganized right now so i'll see if anybody can tell from t$$anonymous$$s first.

 using UnityEngine;
 using System.Collections;
 
 public class CameraFollow : MonoBehaviour 
 {
     public Controller2D target;
     public Vector2 focusAreaSize;
     public float verticalOffset;
     public float lookAheadDistX;
     public float lookSmoothTime;
     public float VerticalSmoothTime;
 
     private FocusArea focusArea;
 
     private float currentLookAheadX;
     private float targetLookAheadX;
     private float lookAheadDirX;
     private float smoothLookVelocityX;
     private float smoothVelocityY;
 
     private bool lookAheadStopped;
 
     public void Start()
     {
         focusArea = new FocusArea(target.box.bounds, focusAreaSize);
     }
 
     public void LateUpdate()
     {
         focusArea.Update(target.box.bounds);
 
         Vector2 focusPosition = focusArea.center + Vector2.up * verticalOffset;
 
         if(focusArea.velocity.x != 0)
         {
             lookAheadDirX = Mathf.Sign(focusArea.velocity.x);
             if(Mathf.Sign(target.playerInput.x) == Mathf.Sign(focusArea.velocity.x) && target.playerInput.x != 0)
             {
                 lookAheadStopped = false;
                 targetLookAheadX = lookAheadDirX * lookAheadDistX;
             }
             else
             {
                 if(!lookAheadStopped)
                 {
                     lookAheadStopped = true;
                     targetLookAheadX = currentLookAheadX + (lookAheadDirX * lookAheadDistX - currentLookAheadX) / 4f;
                 }
             }
         }
 
         currentLookAheadX = Mathf.SmoothDamp(currentLookAheadX,targetLookAheadX,ref smoothLookVelocityX,lookSmoothTime * Time.deltaTime);
 
         focusPosition += Vector2.right * currentLookAheadX;
 
         focusPosition.y = Mathf.SmoothDamp(transform.position.y,focusPosition.y,ref smoothVelocityY,VerticalSmoothTime * Time.deltaTime);
 
         transform.position = (Vector3)focusPosition + Vector3.forward * -10;
     }
 
     public void OnDrawGizmos()
     {
         Gizmos.color = new Color(0,1,0,0.5f);
         Gizmos.DrawCube(focusArea.center,focusAreaSize);
     }
 
     private struct FocusArea
     {
         public Vector2 center;
         public Vector2 velocity;
         private float left, right;
         private float top, bottom;
 
         public FocusArea(Bounds targetBounds, Vector2 size)
         {
             left = targetBounds.center.x - size.x / 2;
             right = targetBounds.center.x + size.x / 2;
             bottom = targetBounds.min.y;
             top = targetBounds.min.y + size.y;
 
             velocity = Vector2.zero;
             center = new Vector2((left+right) / 2 , (top+bottom) / 2);
         }
 
         public void Update(Bounds targetBounds)
         {
             float s$$anonymous$$ftX = 0;
             if(targetBounds.min.x < left)
             {
                 s$$anonymous$$ftX = targetBounds.min.x - left;
             }
             else if(targetBounds.max.x > right)
             {
                 s$$anonymous$$ftX = targetBounds.max.x - right;
             }
             left += s$$anonymous$$ftX;
             right += s$$anonymous$$ftX;
             
             float s$$anonymous$$ftY = 0;
             if(targetBounds.min.y < bottom)
             {
                 s$$anonymous$$ftY = targetBounds.min.y - bottom;
             }
             else if(targetBounds.max.y > top)
             {
                 s$$anonymous$$ftY = targetBounds.max.y - top;
             }
             top += s$$anonymous$$ftY;
             bottom += s$$anonymous$$ftY;
             
             center = new Vector2((left+right) / 2 , (top+bottom) / 2);
             velocity = new Vector2(s$$anonymous$$ftX,s$$anonymous$$ftY);
         }
     }
 }

 
Comment

People who like this

0 Show 0
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

2D Camera Smooth follow, FixedUpdate and LateUpdate odd difference, help needed. 1 Answer

Camera Focus + Follow 0 Answers

Modified SmoothFollow 0 Answers

Locking onto Multiple AI's 1 Answer

why target switching of smooth camera not very smooth ? 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