Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question How to follow mouse cursor with Cinemachine damping?

Discussion in 'Scripting' started by unity_8I_suMy7c9iU0g, Feb 23, 2023.

  1. unity_8I_suMy7c9iU0g

    unity_8I_suMy7c9iU0g

    Joined:
    May 29, 2020
    Posts:
    22
    I have a script to make player look at cursor position
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class AimScript : MonoBehaviour
    6. {
    7.     Vector3 mousePos;
    8.     Rigidbody2D rb;
    9.  
    10.     KatanaCharge chargeScript;
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         rb = GetComponent<Rigidbody2D>();
    16.         chargeScript = transform.GetChild(0).GetComponent<KatanaCharge>();
    17.     }
    18.  
    19.     void FixedUpdate()
    20.     {
    21.         if (chargeScript.charging) return;
    22.  
    23.         mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    24.  
    25.         Vector2 lookDir = mousePos - transform.position;
    26.         lookDir.Normalize();
    27.         float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg - 90;
    28.  
    29.         rb.rotation = angle;
    30.     }
    31. }
    32.  
    The problem is when i set up Cinemachine with damping on camera, the player starts jittering. I figured out that it's a problem caused by damping, and cursor position by itself gets all screwed up. How can i fix it?

    (Interpolation on player enabled, tried setting Cinemachine to work in fixed update or Late update, but doesn't work either)
     
  2. unity_8I_suMy7c9iU0g

    unity_8I_suMy7c9iU0g

    Joined:
    May 29, 2020
    Posts:
    22
    !!!SOLVED!!!

    To anyone who faces this problem, just set up a new object, without any sprite or anything, and just make its transform position follow mouse position. Then make player aim at that object instead of mouse position.

    I do not know why this works, but my guess it has to do something with the way rigidbody is rendered. It also works even better if you add lerp to the object that follows cursor.

    Here's the script for object if anyone wonders:
    Code (CSharp):
    1. public float speed = 20;
    2.  
    3.     Vector2 lerpMouse;
    4.  
    5.     private void Update()
    6.     {
    7.         lerpMouse = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    8.  
    9.         lerpMouse = new Vector2(Mathf.Round(lerpMouse.x * 10.0f) * 0.1f, Mathf.Round(lerpMouse.y * 10.0f) * 0.1f);
    10.         transform.position = Vector2.Lerp(transform.position, lerpMouse, speed * Time.deltaTime);
    11.     }
     
  3. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    717
    You've changed quite a few things with that script! You're rounding to the nearest tenth and lerping the object towards the position, rather than instantly going there.

    It is also possible that you were having problems with checking input during FixedUpdate. FixedUpdate can run zero or more times per frame, and input is checked in Update, iirc.
     
  4. unity_8I_suMy7c9iU0g

    unity_8I_suMy7c9iU0g

    Joined:
    May 29, 2020
    Posts:
    22
    Yeah sorry, i forgot that part. But that and lerp just smoothes out the rotation and prevents some objects of enviroment from jiggling. But the main problem can be solved by a simple
    transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);

    Also i tried moving the part where it gets the position of the cursor to Update or LateUpdate() and setting rotation in FixedUpdate(), but it only started to jitter even more. So this seems like the easiest solution for me, especially after i spent 3-4 hours on this.... ( ꒦ິ﹏ ꒦ິ )