- Home /
The question is answered, right answer was accepted
Make player follow mouse position
I have a space ship in a 2D top down game. Ive been messing around with a script from the unity answers. Here is the script:
using UnityEngine; using System.Collections;
public class playerController : MonoBehaviour { public float speed = 1.5f; private Vector3 target; public Camera PlayerCamera;
void Start()
{
target = transform.position;
}
void Update()
{
if (Input.GetKey(KeyCode.Mouse0))
{
target = PlayerCamera.ScreenToWorldPoint(Input.mousePosition);
target.y = transform.position.y;
target = new Vector3(target.y, transform.position.x, target.z);
transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
}
//transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
}
}
when I click the mouse0 button my player moves a little to the left and stops. I do not know why this is. At the moment I am just trying to get it to follow the mouse but eventually the player will rotate to go a different direction when the mouse position is changed.
Thank you for your help!
Answer by aybeone · Jun 17, 2016 at 04:02 PM
Here's a simple approach that can also move smoothly:
using System;
using UnityEngine;
public class NewBehaviourScript1 : MonoBehaviour
{
private Vector3 _target;
public Camera Camera;
public bool FollowMouse;
public bool ShipAccelerates;
public float ShipSpeed = 2.0f;
public void OnEnable()
{
if (Camera == null)
{
throw new InvalidOperationException("Camera not set");
}
}
public void Update()
{
if (FollowMouse || Input.GetMouseButton(0))
{
_target = Camera.ScreenToWorldPoint(Input.mousePosition);
_target.z = 0;
}
var delta = ShipSpeed*Time.deltaTime;
if (ShipAccelerates)
{
delta *= Vector3.Distance(transform.position, _target);
}
transform.position = Vector3.MoveTowards(transform.position, _target, delta);
}
}
(I did the following setup: added a sprite object and that script to it, then modified main camera to an orthographic projection.)
would this work without a sprite because my spaceship is not a sprite. i will look into the code in a bit.
The only other thing I need to do is have the player point int the direction it is traveling in and tilt it when it turns!
Answer by TheMazinTar · Jun 14, 2016 at 07:18 PM
So, you just want the object to follow the mouse? If that's the case, I'm pretty sure that you only need two of the four lines of movement code that you have.
Try using just these two lines:
target = PlayerCamera.ScreenToWorldPoint(Input.mousePosition);
transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
Or, if you wanted, you could even simplify it further and just go:
transform.position = Vector3.MoveTowards(transform.position, PlayerCamera.ScreenToWorldPoint(Input.mousePosition), speed * Time.deltaTime);
Hope this helps!
Thank you for trying to help! there is another problem though. the player will just move up (a movement i don't want to go in since this is 2D top down)
Okay, I'm a little confused.
From what perspective is your camera to the Unity world coordinates? When you said top down, I just assumed that you meant the style and not necessarily that you've literally positioned the camera and scene to be looking from the top down. There's nothing wrong with doing so, but is this right?
Even so, that's a weird problem . . . By up, you mean along the y-axis, right? Try setting the z-axis of the mouse position, then. Like this:
Vector3 mousePosition = Input.mousePosition;
mousePosition = new Vector3(mousePosition.x, $$anonymous$$athf.Abs(PlayerCamera.transform.position.y), mousePosition.z);
transform.position = Vector3.$$anonymous$$oveTowards(transform.position, PlayerCamera.ScreenToWorldPoint(mousePosition), speed * Time.deltaTime);
The idea here is that the depth into the screen of the mouse (the z-axis that it doesn't automatically have because it's exists in 2D only) matches the distance between the camera and world. Does this at least fix the problem of the player moving up?
$$anonymous$$y camera is rotated so that it looks down on the player. When I said top down i meant literally. I am new at coding and i have recently started on my second game.. I have never used controls like this before so it is very new. By Up I do mean the Y axis in the inspector but for some reason I in my first game I remember it in the actual code being the Z axis in things like new Vector3(x,y,z). In answer to your question It does not fix the problem. I should say though that I have Unity 5.2 not 5.3.5 or 5.4.0. I will get 5.4.0 when the actual version of it comes out to help save my computer space. Thank you again for helping!