- Home /
How do I get the player to correctly rotate toward the mouse?
I have this script called "PlayerController" attached to a cube(the player):
using UnityEngine;
using System.Collections;
[RequireComponent (typeof(CharacterController))]
public class PlayerController : MonoBehaviour {
public float rotationSpeed = 450;
private Quaternion targetRotation;
private CharacterController controller;
private Camera cam;
void Start(){
cam = Camera.main;
controller = GetComponent<CharacterController>();
}
void Update() {
Vector3 mousePos = Input.mousePosition;
mousePos = cam.ScreenToWorldPoint(new Vector3(mousePos.x,mousePos.y,cam.transform.position.y - transform.position.y));
targetRotation = Quaternion.LookRotation(mousePos - new Vector3(transform.position.x, 0, transform.position.z));
transform.eulerAngles = Vector3.up * Mathf.MoveTowardsAngle(transform.eulerAngles.y, targetRotation.eulerAngles.y, rotationSpeed * Time.deltaTime);
Vector3 input = new Vector3(Input.GetAxisRaw("Horizontal"),0,Input.GetAxisRaw("Vertical"));
Vector3 motion = input;
motion *= (Mathf.Abs(input.x) == 1 && Mathf.Abs(input.z) == 1)?.7f:1;
motion += Vector3.up * -8;
controller.Move(motion * Time.deltaTime);
}
}
And I have this script called "GameCamera" attached to the main camera:
using UnityEngine;
using System.Collections;
public class GameCamera : MonoBehaviour {
private Vector3 cameraTarget;
private Transform target;
// Use this for initialization
void Start () {
target = GameObject.FindGameObjectWithTag("Player").transform;
}
// Update is called once per frame
void Update () {
cameraTarget = new Vector3(target.position.x,transform.position.y,target.position.z);
transform.position = Vector3.Lerp (transform.position, cameraTarget, Time.deltaTime * 8);
}
}
The player rotates perfectly when the camera is at 90 degrees in a top-down view, but I want to offset the camera to be 45 degrees and z-18, when ever I do this in the Game camera script like so:
cameraTarget = new Vector3(target.position.x,transform.position.y,target.position.z-18);
and adjust the camera's angle in the Unity inspector to 45 degrees on the X axis - it all looks great until I move the mouse and the player moves in a completely unexpected and unfavorable way.
Any help? Thanks in advance.
Answer by eballin · Mar 19, 2014 at 05:05 AM
Sort of answered my own questions after messing around with a bunch of ideas. Finally just did this after realizing unity coordinates are way different than screen space coordinates(duh).
mousePos.y+Screen.height/3
mousePos = cam.ScreenToWorldPoint(new Vector3(mousePos.x,mousePos.y+Screen.height/3,cam.transform.position.y-transform.position.y));
Might just be a band-aid, though.
Your answer
Follow this Question
Related Questions
Make player face same direction as camera. 4 Answers
Release Mouse button doesent turn script enabled=true again 2 Answers
Rotate the player to face where the mouse is pointing 3 Answers
Camera rotation around player while following. 6 Answers
LookAt once 0 Answers