Search Unity

Question When aiming with raycast can't sync mouse position correctly

Discussion in 'Scripting' started by elpatrongames69, Mar 15, 2023.

  1. elpatrongames69

    elpatrongames69

    Joined:
    Mar 2, 2023
    Posts:
    99
    As you see in the video if i move the mouse fast its aiming correctly but if i move mouse slowly its not aiming.
    How can i fix that?




    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using StarterAssets;
    5.  
    6. public class Aim : MonoBehaviour
    7. {
    8.     StarterAssetsInputs starterAssetsInputs;
    9.     [SerializeField] private LayerMask layerMask;
    10.  
    11.     [SerializeField] private GameObject debugObject;
    12.  
    13.     private void Start()
    14.     {
    15.         starterAssetsInputs = GetComponent<StarterAssetsInputs>();
    16.     }
    17.     private void Update()
    18.     {
    19.         Vector3 mouseWorldPosition = Vector3.zero;
    20.  
    21.         Vector2 screenCenterPoint = new Vector2(Screen.width / 2, Screen.height / 2);
    22.         Ray ray = Camera.main.ScreenPointToRay(screenCenterPoint);
    23.         if (Physics.Raycast(ray, out RaycastHit raycastHit, 999f, layerMask))
    24.         {
    25.             debugObject.transform.position = raycastHit.point;
    26.             mouseWorldPosition = raycastHit.point;
    27.         }
    28.  
    29.         Vector3 worldAimTarget = mouseWorldPosition;
    30.         worldAimTarget.y = transform.position.y;
    31.         Vector3 aimDirection = (worldAimTarget - transform.position).normalized;
    32.  
    33.         transform.forward = Vector3.Lerp(transform.forward, aimDirection, Time.deltaTime * 40);
    34.     }
    35. }
    36.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Is there a Rigidbody on this guy? If so you're bypassing the rotation mechanism by directly manipulating the transform rotation (line 33)... that will trigger physics to reconsider it as a glitch and try to do various "soothings."

    With Physics (or Physics2D), never manipulate the Transform directly. If you manipulate the Transform directly, you are bypassing the physics system and you can reasonably expect glitching and missed collisions and other physics mayhem.

    Always use the .MovePosition() and .MoveRotation() methods on the Rigidbody (or Rigidbody2D) instance in order to move or rotate things. Doing this keeps the physics system informed about what is going on.

    https://forum.unity.com/threads/col...-unity-physic-rigidbody.1216875/#post-7763061

    https://forum.unity.com/threads/oncollisionenter2d-not-being-called.1266563/#post-8044121
     
    elpatrongames69 likes this.
  3. elpatrongames69

    elpatrongames69

    Joined:
    Mar 2, 2023
    Posts:
    99
    No there is no rigidbody in the object upload_2023-3-15_19-33-16.png

    also there is no collider in debug object
    upload_2023-3-15_19-33-42.png
     
  4. elpatrongames69

    elpatrongames69

    Joined:
    Mar 2, 2023
    Posts:
    99
    Any idea how can i fix that?