Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 /
avatar image
0
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 while 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. This enemy is essentially just moving around to various points using lerp. I can't figure out what's causing this. 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 while it's doing this 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 this 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 shiftX = 0;
             if(targetBounds.min.x < left)
             {
                 shiftX = targetBounds.min.x - left;
             }
             else if(targetBounds.max.x > right)
             {
                 shiftX = targetBounds.max.x - right;
             }
             left += shiftX;
             right += shiftX;
             
             float shiftY = 0;
             if(targetBounds.min.y < bottom)
             {
                 shiftY = targetBounds.min.y - bottom;
             }
             else if(targetBounds.max.y > top)
             {
                 shiftY = targetBounds.max.y - top;
             }
             top += shiftY;
             bottom += shiftY;
             
             center = new Vector2((left+right) / 2 , (top+bottom) / 2);
             velocity = new Vector2(shiftX,shiftY);
         }
     }
 }

 
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

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

Camera Focus + Follow 0 Answers

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

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