- Home /
2d Space Flight Movement Script
I'm running into a lot of problems coding, hopefully some one can give me some pointers on things i need to look out for when i do this, or functions i can use that'll make this easier, but essentially what im trying to do is make a 2d game that works with the physics youd find in space, and that uses click to move.
that means: click &hold to move - clicking sets the destination and the time held is how long you're accelerating for.
releasing means that you will continue to travel along that vector at your final speed forever (or until you hit something)
clicking also should rotate the player's ship so that its looking at the point its trying to approach.
the code im using right now (ripped from a wiki) looks like this :
// Click To Move script // Moves the object towards the mouse position on left mouse click var smooth:int; // Determines how quickly object moves towards position private var targetPosition:Vector3; function Update () { if(Input.GetKey(KeyCode.Mouse0)) { var playerPlane = new Plane(Vector3.up, transform.position); var ray = Camera.main.ScreenPointToRay (Input.mousePosition); var hitdist = 0.0; if (playerPlane.Raycast (ray, hitdist)) { var targetPoint = ray.GetPoint(hitdist); targetPosition = ray.GetPoint(hitdist); var targetRotation = Quaternion.LookRotation(targetPoint - transform.position); transform.rotation = targetRotation; } } transform.position = Vector3.Lerp (transform.position, targetPosition, Time.deltaTime * smooth); }
The rotation function works beautifully, and it will continue to advance toward the location of the mouse click - but ONLY for as long as the click is held down, if the player releases the mouse click the ship stops instantly. further - the ship seems to start out very quickly (in relation to how far it is from the mouse) and slows down a lot as it approaches the location of the mouse click.
I'm really not very good at programming, any help anyone can give would be awesome. I don't expect people to code this for me but if people can point out ways to make the ship continue moving along a vector toward the mouse click, rather than to the mouse click - or can say how to use an acceleration rather than use distance to the target to determine speed that would be fantastic.
Answer by robertbu · Sep 25, 2013 at 03:01 AM
If you want realistic space physics, then consider using a Rigidbody and AddForce(). But I'm not sure it is what you want, or if your mechanism of aiming and accelerating will work well if you do want realistic physics. You say the turning works well, but the turning does not reflect real physics. In real physics, when you add force it does not immediately cancel out the existing velocity and head in a new direction. In addition your current code will immediate jump to a new rotation. Here is some code that implements a simplified form of real physics using your turn and force model:
var rotationSpeed = 1.0;
var force = 3.0;
private var targetPosition:Vector3;
private var velocity = Vector3.zero;
function Update () {
if(Input.GetMouseButton(0))
{
var pos = Input.mousePosition;
pos.z = Camera.main.transform.position.y;
pos = Camera.main.ScreenToWorldPoint(pos);
var targetRotation = Quaternion.LookRotation(pos - transform.position);
velocity += transform.forward * Time.deltaTime * force;
}
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
transform.position += velocity * Time.deltaTime;
}
And here is a modification that turns the direction and velocity immediately like your current model. It also projects the current velocity onto the forward vector, so you can slow down by turning the ship:
#pragma strict
var rotationSpeed = 1.0;
var force = 3.0;
private var targetPosition:Vector3;
private var velocity = Vector3.zero;
function Update () {
if(Input.GetMouseButton(0))
{
var pos = Input.mousePosition;
pos.z = Camera.main.transform.position.y;
pos = Camera.main.ScreenToWorldPoint(pos);
var targetRotation = Quaternion.LookRotation(pos - transform.position);
transform.rotation = targetRotation;
velocity = Vector3.Project(velocity, transform.forward);
velocity += transform.forward * Time.deltaTime * force;
}
transform.position += velocity * Time.deltaTime;
}
Thank you so much, this is exactly the help I was hoping for and I can definitely alter this code for my needs. Id upvote your comment but I don't have the rep for it yet
i want the script but can u please remove rotation bcuz its an 2d sprite