Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 monserboy · Apr 12, 2014 at 04:06 PM · enemyvelocitylinearjerkyjittery

Jerky velocity movement

Hi

I'm making a vertical scroller 2D game that contains a crow as enemy that flies directly to the position the player is residing at.

The crow is a prefab that is instantiated in a game pool at start of the game. Via the following script the crow rotates toward the player and flips if necessary so the gameobject is in the correct direction. Then it moves towards the player in a linear motion until it collides with an invisible wall.

However my following code produces an annoying jerky movement when playing my game on high resolution devices. The problem is not reproducible in my editor.

  • I've tried different Time velocities and placing the object in FixedUpdate (since it's using physics).

  • I've also tried chaning the interpolation of the crow.

  • I've tried increasing my velocity and position iterations in the physics2D project settings.

This is the code I use to let the crow fly from the top left/right to the player:

 using UnityEngine;
 using System.Collections;
 
 public class CrowFollow : MonoBehaviour {
 
     private GameObject targetToHit;
     private Vector3 playerObjectPos, enemyStartPosition;
     private float enemyStartPositionX;
     private Transform enemyTransformCache;
     private Vector3 transformRightCache;
 
     void Awake(){
         targetToHit = GameObject.FindWithTag("Player");
     }
 
     void Start(){
         FlipEnemyHorizontal();
     }
 
     void OnEnable(){
         enemyStartPositionX = gameObject.transform.position.x;
         enemyStartPosition = gameObject.transform.position;
         enemyTransformCache = gameObject.transform;
         playerObjectPos = targetToHit.transform.position;
         RotateEnemy ();
         transformRightCache = transform.right;
     }
 
     void Update(){
         MoveEnemy ();
     }
 
     void MoveEnemy(){
         if (enemyStartPositionX > 0){
             rigidbody2D.velocity = -transformRightCache*220*Time.smoothDeltaTime;
         } else {
             rigidbody2D.velocity = transformRightCache*220*Time.smoothDeltaTime;
         }
     }
 
     void FlipEnemyHorizontal(){
         //This flips the enemy horizontal towards the target
         if (enemyStartPositionX > 0) {
             enemyTransformCache.localScale = new Vector3(-enemyTransformCache.localScale.x, enemyTransformCache.localScale.y, enemyTransformCache.localScale.z);
         }
     }
     
     void RotateEnemy(){
         //Let the crow look at the player and rotate towards it
         if (targetToHit.transform.position.x < 0.00000010){
             Quaternion rotation = Quaternion.LookRotation(playerObjectPos - enemyTransformCache.position, enemyTransformCache.TransformDirection(Vector3.up));
             enemyTransformCache.rotation = new Quaternion(0, 0, rotation.z, rotation.w);
         } else if (targetToHit.transform.position.x > 0.00000011){
             Quaternion rotation = Quaternion.LookRotation(playerObjectPos - enemyTransformCache.position, enemyTransformCache.TransformDirection(Vector3.up));
             enemyTransformCache.rotation = new Quaternion(0, 0, rotation.z, rotation.w);
         }
     }
 
     void OnCollisionEnter2D(){
 
         gameObject.SetActive(false);
         gameObject.transform.position = enemyStartPosition;
     }
 
 
 }

  • I'm testing my game on a Google/LG Nexus 5 (1920x1080 pixels)

  • Unity version: 4.3.4 pro

  • The crow has a rigidbody2D with no linear or angular drag and gravity scale set to 0

  • The crow has a sprite renderer with an image size of 128x77 pixels

I'm really out of options here. I've read numerous different forum posts and answers but none seem to help me out here...

Thanks for the help in advance!

~monsterboy

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by monserboy · Apr 13, 2014 at 10:17 AM

I managed to fix this problem by checking the VBlank option in my project settings. This option will force the game in a sort of VSync mode that also enforces the game to run at 60fps.

I had a secondary scene script that stated that the game should use a fps of 120 to ensure max smoothness, this however wasn't the case with my crow enemy. I changed this value to 60 , added the whole movement script to FixedUpdate() and now everything is smooth without using the VBlank option.

Weird...

Comment
Add comment · Share
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
avatar image
2

Answer by king_ · Apr 12, 2014 at 06:10 PM

Try using FixedUpdate() rather rhan Update().... try and reply

Comment
Add comment · Show 1 · Share
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
avatar image monserboy · Apr 12, 2014 at 06:28 PM 0
Share

I've tried that but forgot to mention it. Thanks though :)

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

22 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

Related Questions

Velocity powered rigidbody on a moving platform without parenting. 3 Answers

Rigidbody enemy Forcemode.VelocityChange 1 Answer

Jerky/jittery object movement 1 Answer

My bullet has no velocity after it is spawned. 1 Answer

Help fixing AI? 1 Answer


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