- Home /
Jittery Movement (2D)
Yeah, so basically I'm making a 2D Sideview shooter game similar to Gradius or Cuphead (Plane levels) and no matter what I do I'm getting jittery and weird looking movement for both my witch MC and enemies. I've looked through other unity answers and have found people with similar problems but none of them are exactly the same and none of the solutions have worked so far. There are no physics and the camera is always completely stationary.
Enemy Movement Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyMovement : MonoBehaviour
{
public GameObject Enemy;
public float Movespeed = 15f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void FixedUpdate()
{
Enemy.transform.position = new Vector2(
transform.position.x + (Movespeed * Time.deltaTime),
transform.position.y
);
}
}
Character Movement Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Movement : MonoBehaviour
{
public Joystick Joystick;
public GameObject Witch;
public float Speed = 10f;
public Button FireButton;
public GameObject MagicSP;
public GameObject MagicBullet;
// Start is called before the first frame update
void Start()
{
FireButton.onClick.AddListener(TaskOnClick);
}
// Update is called once per frame
void Update()
{
//Movement
Vector2 pos = transform.position;
//Veritcal
pos.y += Joystick.Vertical * Speed * Time.deltaTime;
transform.position = pos;
//Horizontal
pos.x += Joystick.Horizontal * Speed * Time.deltaTime;
transform.position = pos;
}
void TaskOnClick()
{
Instantiate(MagicBullet, MagicSP.transform.position, MagicSP.transform.rotation);
}
}
I'm kind of a noob to this so sorry if its a stupid question but I'd appreciate any and all advice. Thanks! :)
Answer by rufopufo · Jan 10, 2020 at 09:22 AM
Hi there,
Probably you are getting that weird movement because you are changing directly x and y components.
It may help you if you use Translate() method, as following: Enemy.transform.Translate(Movespeed*Time.deltaTime,0,0);
And do the same for the Player. This Translate method just moves along the x, y or z axis, as much units as you set there.
Btw, I personally prefer to generate the player movement with the Rigidbody component, instead of using the transform component. But I don't really know even if you are using rigidbodies... So I answered the question using the transform component, as you had in your code.
Hope it helps.
Answer by LukeMildenberger · Jan 14, 2020 at 02:29 AM
Hi rufopufo, thanks so much for the answer! Unfortunately I'm still getting the jittery movement. I'm starting to think that it might not be the scripts but instead a setting with Unity or my hardware. My first thought was that it might be the refresh rate of my monitor, but then I tried it with another monitor and then a TV and I still got the same results. I also tweaked the fixed timestep setting in Unity because I saw something about it in another Unity Answers post, but the problem persisted. any advice would be helpful, thanks! :)
Here is a video if it helps: Youtube Video
Answer by Marioooo · Jan 14, 2020 at 06:32 AM
If there's no physics involved so you made your own physics system.
There's a platformer tutorial in unity which involves creating your own physics system... Take a look at it... Also I don't see your physics system so how do you check wherever you're hitting something? Maybe that jittery movement us caused by moving the object and then calculating physics about where's ground.
Use a raycast or a collider casting to see if the gravity must be applied or not...
Hope it helps
Hi $$anonymous$$arioooo Thanks for your answer! The reason why I don't have a physics system is because there is no gravity or ground . In my game you play as a witch on a broom flying through the air, which is why I didn't add any physics. Think of the game play like Gradius or the plane levels in Cuphead. Thanks for any advice!
In that case you can still use RIGIDBODY 2D. Just set gravity scale 0, set a drag, freeze rotation and use Rigidbody2D.AddForce(vector2D, Force$$anonymous$$ode.Impulse) To move anywhere you want and after adding the force, clamp velocity to $$anonymous$$ax movement speed. It'll be smoother and easier.
Thanks again for the reply marioooo! I did what you said and it helped a lot! unfortunately there is still a sort of vibration/jittery movement with the enemies. I honestly have no idea what to do, I've tried so much but to no avail. if you have any other ideas I would love to hear them. Thanks :)
Answer by ArKeid0s · Jun 18, 2021 at 07:35 AM
Did you manage to fix your problem ? I have the same problem