- Home /
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
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...
Answer by king_ · Apr 12, 2014 at 06:10 PM
Try using FixedUpdate()
rather rhan Update()
.... try and reply
I've tried that but forgot to mention it. Thanks though :)
Your answer
Follow this Question
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