- Home /
Player jitters when moving.
Help!! So I have been suffering with this for the past few days. I'm not sure if I should redo the code or what, but I have made so much progress so far so I'm uncertain about doing that. I don't want to wait to long to fix this either
The goal is to have a player shoot while facing the direction of a crosshair. The player itself is shooting though, not a pistol coming from a player. The problem is, the player shakes when it moves, even though it's able to shoot perfectly. I believe the problem is that the player rotates in one script file while the player moves in the other. Let me show you:
Connected to the camera object:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PointAndShoot : MonoBehaviour
{
bool allowfire = true;
public GameObject crosshairs, player, bulletPrefab, b;
public float fireRate, bulletSpeed, currentAmmo, maxAmmo, AmountOnScreen, rotationZ;
private Vector3 target;
Vector3 difference;
// Use this for initialization
void Start()
{
currentAmmo = maxAmmo;
Cursor.visible = false;
}
// Update is called once per frame
void Update()
{
if (player != null)
{
if (Cursor.visible == false)
{
target = transform.GetComponent<Camera>().ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, transform.position.z));
crosshairs.transform.position = new Vector2(target.x, target.y);
difference = target - player.transform.position;
rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
player.transform.rotation = Quaternion.Euler(0.0f, 0.0f, rotationZ);
}
if (Cursor.visible == true)
{
target = new Vector3(0, 0, 0);
player.transform.rotation = Quaternion.identity;
}
if (Input.GetMouseButtonDown(0) && allowfire)
{
float distance = difference.magnitude;
Vector2 direction = difference / distance;
direction.Normalize();
StartCoroutine(fireBullet(direction, rotationZ));
Debug.Log("Ammo: " + currentAmmo);
}
}
IEnumerator fireBullet(Vector2 direction, float rotationZ)
{
if (currentAmmo > 0)
{
allowfire = false;
b = Instantiate(bulletPrefab, player.transform.position, Quaternion.identity) as GameObject;
//b.transform.position = player.transform.position;
//b.transform.rotation = Quaternion.Euler(0.0f, 0.0f, 0.0f);
b.GetComponent<Rigidbody2D>().velocity = direction * bulletSpeed;
yield return new WaitForSeconds(fireRate);
currentAmmo--;
allowfire = true;
}
}
}
public float AmmoToGive(float ammoGiven)
{
if(currentAmmo < maxAmmo)
{
if(ammoGiven <= 0)
{
return ammoGiven;
}
if(currentAmmo + ammoGiven <= maxAmmo)
{
currentAmmo += ammoGiven;
return 0;
}
//get the difference between maxAmmo and currentAmmo
float diff = maxAmmo - currentAmmo;
//difference added to current ammo
currentAmmo += diff;
Debug.Log("Received " + diff + " bullets.");
//return the ammo that wasn't used
return (ammoGiven - diff);
}
return ammoGiven;
}
}
Connected to the Player:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerMovement : MonoBehaviour
{
//public Image healthBar;
public int playerMaxHealth = 100;
public int playerCurrentHealth;
public int dmg;
public float mSpeed;
Rigidbody2D rbody;
public Slider healthbar;
public bool alive;
// Start is called before the first frame update
void Start()
{
playerCurrentHealth = playerMaxHealth;
rbody = GetComponent<Rigidbody2D>();
alive = true;
}
// Update is called once per frame
void Update()
{
//movement
float moveX = Input.GetAxis("Horizontal");
float moveY = Input.GetAxis("Vertical");
Vector2 move = new Vector2(moveX,moveY);
rbody.velocity = move * mSpeed;
healthbar.value = playerCurrentHealth;
}
void OnCollisionEnter2D(Collision2D other)
{
if (other.collider.CompareTag("Enemy"))
{
int health = GetDamaged(dmg);
Debug.Log("Health is now:" + health);
if (health <= 40 && health != 0) {
Debug.Log("Warning, critical damage!");
}
if (health <= 0) {
healthbar.value = 0;
Destroy(gameObject);
alive = false;
Time.timeScale = 0;
Debug.Log("Game over.");
}
}
}
public int GetDamaged(int attackdmg)
{
playerCurrentHealth -= attackdmg;
return playerCurrentHealth;
}
}
Answer by muhammadtahiriqbal · Jun 07, 2020 at 07:25 AM
First Problem in your i found that is, you are applying velocity in Update it should be applied in FixedUpdate function are you sure about that is player jittering or camera
That was the solution to my problem. I had to move the Quaternion.Euler upwards to FixedUpdate(). Then I had to reset interpolation to interpolation. It's moving well now. Thank you! :)
Your answer
Follow this Question
Related Questions
Cinemachine camera rotating with player 2D 1 Answer
Script that stays behind player that rotates the player along with the camera/mouse (third person) 0 Answers
Camera Rotating when the player reach a specific position 0 Answers
Rotation of player based on camera direction and joystick direction in 3D world 1 Answer