- Home /
Question by
awplays49 · May 11, 2015 at 09:27 PM ·
mouse look
Top down shooter mouse look (3d)
How can i make a top down shooter mouse look? (xz movement, y rotation) this is what i have:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
private Vector3 mousePos;
private float lastAngle;
void Update () {
mousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
transform.LookAt (new Vector3 (mousePos.x, transform.position.y, mousePos.y));
}
}
Comment
Best Answer
Answer by WillNode · May 11, 2015 at 09:47 PM
not testing this script, but i hope this one is working well.
void Update () {
mousePos = Camera.main.ScreenToWorldPoint (new Vector3(Input.mousePosition.x,Input.mousePosition.y, Camera.main.transform.position.z-transform.position.z));
transform.LookAt (mousePos);
}
if not working, maybe this one :
void Update () {
mousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
transform.LookAt (new Vector3 (mousePos.x, transform.position.y, mousePos.z));
}
Answer by BarthaSzabolcs · May 04, 2021 at 06:03 PM
I know it's an old post, but this is still a valid problem.
I made a 3 minute tutorial about this with explanation and an example project.
My Solution
using UnityEngine;
namespace BarthaSzabolcs.IsometricAiming
{
public class IsometricAiming : MonoBehaviour
{
#region Datamembers
#region Editor Settings
[SerializeField] private LayerMask groundMask;
#endregion
#region Private Fields
private Camera mainCamera;
#endregion
#endregion
#region Methods
#region Unity Callbacks
private void Start()
{
// Cache the camera, Camera.main is an expensive operation.
mainCamera = Camera.main;
}
private void Update()
{
Aim();
}
#endregion
private void Aim()
{
var (success, position) = GetMousePosition();
if (success)
{
// Calculate the direction
var direction = position - transform.position;
// You might want to delete this line.
// Ignore the height difference.
direction.y = 0;
// Make the transform look in the direction.
transform.forward = direction;
}
}
private (bool success, Vector3 position) GetMousePosition()
{
var ray = mainCamera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out var hitInfo, Mathf.Infinity, groundMask))
{
// The Raycast hit something, return with the position.
return (success: true, position: hitInfo.point);
}
else
{
// The Raycast did not hit anything.
return (success: false, position: Vector3.zero);
}
}
#endregion
}
}
Your answer
Follow this Question
Related Questions
Explaining A Mouse Look Script 1 Answer
gyro to mouse look 0 Answers
My camera flickers and it's driving me crazy 2 Answers
Main Camera won't rotate left or right 0 Answers
"Ref Float" for Mouse Look script? 1 Answer