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.