- Home /
Problems with accurate third-person mouse aiming
Basically, I'm trying to create third-person aiming that applies regardless of where the player character is on the screen (for example, slightly to the left of the camera centre), but for some reason, no matter what methods I use, the end result is highly inaccurate (note that this also applies to mouse aiming independent of camera movement), aiming completely away from where the cursor is pointing at.
This is my current and probably most accurate iteration of the code:
void attackRoutines()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Vector3 lookPos;
RaycastHit hit;
float defaultAimDistance = 200.0f;
if (Input.GetButton("Fire1"))
{
if (Physics.Raycast(ray, out hit, 1000.0f))
{
lookPos = hit.point;
Debug.DrawLine(firingPoint.position, hit.point, Color.red, Time.deltaTime, false);
}
else
{
//lookPos = Input.mousePosition;
//lookPos.z = defaultAimDistance;
//lookPos = Camera.main.ScreenToWorldPoint(lookPos);
lookPos = ray.origin + ray.direction * 200.0f;
Debug.DrawLine(firingPoint.position, lookPos, Color.blue, Time.deltaTime, false);
}
if (Time.time > (shotTimer + shotDelay))
{
GameObject clone;
clone = (GameObject)Instantiate(regularProjectile, firingPoint.position, firingPoint.rotation);
Physics.IgnoreCollision(clone.collider, collider); Physics.IgnoreCollision(clone.collider, rider.collider);
clone.transform.LookAt(lookPos);
shotTimer = Time.time + shotDelay;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Spotlight cookie causes light leaks when camera is far away 0 Answers
Reticle orbits around player depending on right stick direction? 0 Answers
2.5d game gun aim 1 Answer
Aiming With my mouse 1 Answer
my gun wont follow my mouse 0 Answers