Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Unity 2d camera jitter

Discussion in 'Getting Started' started by Ishaan_J, Apr 16, 2024.

  1. Ishaan_J

    Ishaan_J

    Joined:
    Apr 16, 2024
    Posts:
    1
    Unity 2d camera jitter.

    Camera -
    using JetBrains.Annotations;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Camera_Controller : MonoBehaviour
    {
    public Rigidbody2D player;
    public Transform healthbar;
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void FixedUpdate()
    {
    //transform.position = player.position + player.up * 0.7f;
    Vector3 difference = (Vector3)player.position + (Vector3) player.velocity * 0.1f * player.velocity.magnitude - transform.position;
    transform.position += difference / 10;
    healthbar.position = transform.position + new Vector3(0.7f,1.3f,0);
    }
    }

    Car -
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using Microlight.MicroBar;
    using DG.Tweening;

    public class Car_Controller : MonoBehaviour
    {
    public Rigidbody2D rb;
    public float health = 25;
    //public Camera cam;
    public MicroBar hpBar;
    public Animator explosion;
    public Transform explosionTransform;
    public GameObject explosionObject;
    public GameObject gameOverscreen;
    private int timer = 0;
    private bool GameOver = false;

    // Start is called before the first frame update
    void Start()
    {
    hpBar.Initialize(100f);
    explosionObject.SetActive(false);
    gameOverscreen.SetActive(false);
    }

    // Update is called once per frame
    void FixedUpdate()
    {
    if (!GameOver)
    {
    if (Input.GetKey(KeyCode.W))
    {
    rb.AddRelativeForce(Vector3.up * 6f);
    }
    if (Input.GetKey(KeyCode.A))
    {
    rb.AddTorque(2f);
    }
    if (Input.GetKey(KeyCode.S))
    {
    rb.AddRelativeForce(Vector3.down * 6f);
    }
    if (Input.GetKey(KeyCode.D))
    {
    rb.AddTorque(-2f);
    }
    if (Input.GetKey(KeyCode.K))
    health = 0;
    }
    explosionTransform.position = transform.position;
    timer--;
    if (timer <= 0)
    explosionObject.SetActive (false);
    gameOverscreen.transform.position = transform.position;
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
    if (collision.relativeVelocity.magnitude > 2.2) {
    health -= collision.relativeVelocity.magnitude;
    hpBar.UpdateHealthBar(health*4);
    Debug.Log(health);
    explosionObject.SetActive(true);
    timer = 45;

    if (health <= 0)
    {
    GameOver = true;
    explosionObject.transform.localScale = 2*Vector3.one;
    gameOverscreen.SetActive(true);
    gameOverscreen.transform.localScale = 0.3f*Vector3.one;
    gameObject.GetComponent<Renderer>().enabled = false;
    hpBar.gameObject.SetActive(false);

    }
    }
    }
    }