- Home /
Rotate the player to face where the mouse is pointing
I know very similar questions have been asked and, knowing unity, the exact question I'm asking has been asked. I've looked on unity answers for 2 days looking for an answer but I haven't found it,if there is already an answer out there just link it to me. I am making a 3rd person game where the player controls a space ship. I currently control the rotation of the ship with the keyboard, but I want to control it with the mouse. I have found a script on unity answers that makes the ship literally face the mouse, but I want the ship to face where the mouse is pointing. The current script I have means that if I put my mouse in the middle of the screen, the ship faces me, this is not what I want. If my mouse where in the middle of the screen I want the ship to face directly away from me, at the mouse as if the mouse is further away from the camera than the ship. Here is what I have so far:
var lookFactor = 0.8;
function Update () {
var distance = (transform.position.z - Camera.main.transform.position.z)*lookFactor;
var position = Vector3(Input.mousePosition.x, Input.mousePosition.y, distance);
position = Camera.main.ScreenToWorldPoint(position);
transform.LookAt(position);
}
But like I said this makes the ship face the cursor, not where the mouse is pointing in the world. Any help appreciated.
Answer by ArkaneX · Sep 12, 2013 at 06:29 PM
Actually it's very easy - change lookFactor to value greater than 1 :)
This way, your distance variable will be greater than z distance from camera to transform, while now it is lower.
Although this does work, it is not entirely what I need. I also want my player to be able to shoot (which I have already done). But, lets say they have some sort of beam weapon, this weapon has 2 turrets, one on each wing. When the player shoots, these 2 beam turrets point at the mouse and there is no overlap, the 2 beams meet perfectly at the mouse. But how far away from the ship they meet (with the original script with the change mentioned above) depends on the lookFactor (correct me if this statement is wrong). What if the lookFactor is too small and the beams meet and end before they reach the enemy, or its too far, the beams meet past the enemy but both miss the enemy (one goes either side). I understand there's a mouseToWorld (or something like that) function that couls be used to make the ship not point right at the cursor but where the mouse is pointing in the world, but I don't know how to use it in a case like this. Thanks for your input nonetheless. Also is there a way to make the rotation happen over time with a variable turnSpeed?
@empire2202 - when posting answers, I usually try to take into account what is written in the question. It's hard to guess what the OP had in $$anonymous$$d, if he didn't state it clearly.
Anyway - you mentioned mouseToWorld or similar function, but you meant ScreenToWorld function, which you're already using. Output of this script is a point in 3D world, and your ship points exactly at it.
And regarding the weapons. I would shoot the beams straight ahead and make their destination far away (1000 units, maybe more - this depends on your need). I don't understand why do you want them to meet? Do you want guns of your ship to target only one point in space?
Please attach line renderer component to your ship, and test this code:
private var laserBeam : LineRenderer;
function Start () {
laserBeam = GetComponent.<LineRenderer>();
laserBeam.SetWidth(0.1f, 0.1f);
laserBeam.SetPosition(0, transform.position);
laserBeam.SetPosition(1, transform.position);
}
var lookFactor = 0.8;
function Update () {
var distance = (transform.position.z - Camera.main.transform.position.z)*lookFactor;
var position = Vector3(Input.mousePosition.x, Input.mousePosition.y, distance);
position = Camera.main.ScreenToWorldPoint(position);
transform.LookAt(position);
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.Space))
{
var endPoint = transform.forward * 1000;
laserBeam.SetPosition(0, transform.position);
laserBeam.SetPosition(1, endPoint);
}
if(Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.Space))
{
laserBeam.SetPosition(0, transform.position);
laserBeam.SetPosition(1, transform.position);
}
}
It's not what you want, because this is one laser only, shooting from center of the ship, but you can build your solution on top of it.
thanks for the help with beam lasers, but allow me to attempt to rephrase my question regarding looking at the mouse (it's quite hard to explain). I have the ship able to shoot a missile in the direction the ship is facing. I put my mouse towards the upper-left section of the screen. I fire my weapon and the missile ends up going in a direction which is slightly below and to the right of the cursor. I know JavaScript but am somewhat new to unity(script). The way it should work is I shoot and so long as I don't move the mouse, the missile moves straight to the mouse until it reaches its max range and despawns, but like I said it is going a bit below and to the right of the cursor. It's a bit difficult to explain exactly what I want, if you look at a game called Star Conflict, I'm looking for similar controls to that (Note: I've already setup my controls, I only need the look at mouse to work properly, I used Star Conflict as an example because it's quite hard to explain)
In other words, I need the depth, it's acting as if its 2d, looking directly at the cursor, it needs to shoot at where the mouse is pointing in the world (sorry if this didn't make sense)
At first I thought that you didn't get something, but then I think I noticed why. In my ship I had lookFactor set to 1000 and this causes quite different output :)
Please change the line
var endPoint = transform.forward * 1000;
to
var endPoint = transform.forward * lookFactor;
and then try to set value of lookFactor in inspector to 2, then 10, 100, 1000... You will see that it works. You can also comment the second line in Start method. This will cause line to have default width of 1 and you'll see that its width will change from the start to the end in 3D world.
ah yes, I see why it's acting how it is, thanks. I also need some help modifying this method to other weapons like missiles which I've already got working. The way it currently works is, I click the left mouse button and a prefab of a missile is instantiated then force is applied to its rigid body to propel it forward. But I want to change it so when I click, it instantiates a missile facing the mouse (like you did with the beam weapons) then apply force to the rigid body to propel it forward. How do I make sure it shoots with a fixed range and reaches the cursor as it exceeds its range and despawns.
Lets say I put my mouse on an enemy ship 500m away, but the code is set to act as if the mouse is 1000m away (and I'm not ai$$anonymous$$g directly forward), this would cause the missile to miss despite the cursor appearing to be directly ontop the ship. Is it possible to and if so how do I adjust the way the code treats the mouse (the distance the mouse is from the camera) to compensate for this (hope this made sense). I haven't tested it but I also think it would still act this way with the beam weapon.
Answer by empire2202 · Sep 14, 2013 at 07:45 AM
thanks for your help and the links, ArkaneX found the answer in my original code, the look factor was too small
var lookFactor = 10;
function Update () {
var distance = (transform.position.z - Camera.main.transform.position.z)*lookFactor;
var position = Vector3(Input.mousePosition.x, Input.mousePosition.y, distance);
position = Camera.main.ScreenToWorldPoint(position);
transform.LookAt(position);
}
I am now asking another question (http://answers.unity3d.com/questions/536425/weapon-targeting-in-3d-space-using-the-2d-mouse.html) regarding mouse controlled weapons in 3d space
Great! If you're done here, please mark whatever answer was best for you as correct. (You can mark your own btw)
@vexe - while marking own answer is ok, I think doing it in case when answer is directly derived from other user answer and comments is just not right.
Ahh yea I'm with you on that. I didn't actually read your solution but the reason I said that is because he mentioned that he found the answer in his original code, I thought it's ok.
Your answer
Follow this Question
Related Questions
How do I get the player to correctly rotate toward the mouse? 1 Answer
Camera rotation around player while following. 6 Answers
Rotate to face player, drunk issues 0 Answers
Character always facing mouse cursor position... 3 Answers
Make game object face player and at the same time follow mouse position 2 Answers