- Home /
How do I make my player face the mouse on the Y axis in Unity C# 2D?
I have a c# script to make my player move towards the mouse but I need it to rotate to face the mouse. I have never dealt with these types of controls before and I've been trying for a long time to figure it out.
Here is my script:
using UnityEngine; using System.Collections;
public class playerController : MonoBehaviour { private Vector3 _target; public Camera Camera; public bool FollowMouse; public bool ShipAccelerates; public float ShipSpeed = 2.0f;
void Start()
{
}
public void OnEnable()
{
if (Camera == null)
{
throw new System.InvalidOperationException("Camera not set");
}
}
public void Update()
{
if (FollowMouse || Input.GetMouseButton(0))
{
_target = Camera.ScreenToWorldPoint(Input.mousePosition);
_target.y = 0;
}
var delta = ShipSpeed * Time.deltaTime;
if (ShipAccelerates)
{
delta *= Vector3.Distance(transform.position, _target);
}
transform.position = Vector3.MoveTowards(transform.position, _target, delta);
}
thank you so much for helping!
Answer by Cherno · Jun 27, 2016 at 07:32 PM
public Vector3 lookAtTargetPos;
void Update() {
lookAtTargetPos= Camera.main.ScreenToWorldPoint(Input.mousePosition);
if(lookAtTargetPos != Vector3.zero) {
Quaternion rot = Quaternion.LookRotation(transform.position - lookAtTargetPos, Vector3.up);
transform.rotation = rot;
transform.eulerAngles = new Vector3 (0, transform.eulerAngles.y, 0);
}
}
Thanks for answering. I had to modify Camera.main.ScreenToWorldPoint(Input.mousePosition);
to
Camera.ScreenToWorldPoint(Input.mousePosition);
because my camera is attached to my player (also note it is orthographic). When I changed the line of code my player rotated constantly very rapidly though at least it was only on the Y axis. Thank you again for answering and I hope this problem is solved quickly!
I changed one thing and it all worked!
Quaternion rot = Quaternion.LookRotation(transform.position - lookAtTargetPos, Vector3.up);
to
Quaternion rot = Quaternion.LookRotation(transform.position + lookAtTargetPos, Vector3.up);
Yay!!!!!!!!!
There is one problem though. It will not turn when the mouse is behind the player and then when the mouse is in certain parts of the screen it will spin very rapidly.
So I took my player camera off the player and it works but it does not turn fast enough. also I have a particle emitter as an engine and that won't rotate with the player. I will also need it to tilt on the Z axis as it turns to signify a tilting action. (sorry i didn't say it at first but i just wanted to get through that)
Set the ParticleSystem's simulation space to local. Why it won't fast enough is beyond my understanding since the rotation is applied every frame, which is as fast as possible.
the simulation space actually is set to local. what I mean by rotate fast enough is that it needs to look like it is flying almost. (except it is in space)
Your answer
Follow this Question
Related Questions
Synchronising the rotation around Y axis of two objects 1 Answer
Rotation drift with transform.eulerAngles 0 Answers
Object won't rotate 2 Answers