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?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using StarterAssets;
public class Aim : MonoBehaviour
{
StarterAssetsInputs starterAssetsInputs;
[SerializeField] private LayerMask layerMask;
[SerializeField] private GameObject debugObject;
private void Start()
{
starterAssetsInputs = GetComponent<StarterAssetsInputs>();
}
private void Update()
{
Vector3 mouseWorldPosition = Vector3.zero;
Vector2 screenCenterPoint = new Vector2(Screen.width / 2, Screen.height / 2);
Ray ray = Camera.main.ScreenPointToRay(screenCenterPoint);
if (Physics.Raycast(ray, out RaycastHit raycastHit, 999f, layerMask))
{
debugObject.transform.position = raycastHit.point;
mouseWorldPosition = raycastHit.point;
}
Vector3 worldAimTarget = mouseWorldPosition;
worldAimTarget.y = transform.position.y;
Vector3 aimDirection = (worldAimTarget - transform.position).normalized;
transform.forward = Vector3.Lerp(transform.forward, aimDirection, Time.deltaTime * 40);
}
}
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.
You say you're not using 2D physics but you're using queries therefore obviously colliders. This IS using 2D physics therefore you need to use it correctly so here are some basic rules:
Don't ever modify the Transform if using 2D physics components
If you want a 2D Collider to move then add a Rigidbody2D and use its API to move it; many ways to do this.
Rigidbody2D don't make it "unresponsive". If you want explicit control then set the body-type to Kinematic; it's what it's for.
When you qu…
That’s not related to body-type. You should never drive via the Transform. The main thing to understand here is that the Rigidbody2D role is to write to the Transform after the physics simulation, not the other way around. It acts as a proxy to the Transform because you want physics behaviour.
Does “it work” if you write to the Transform and force the Rigidbody2D to read from the Transform? Yes! It works like that because otherwise it’d be reported as a bug and so we’re forced to “support” it.…
1 Like
Kurt-Dekker:
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.
Collisions in high speed without unity physic / rigidbody
OnCollisionEnter2D not being called
No there is no rigidbody in the object
also there is no collider in debug object
Any idea how can i fix that?