- Home /
How do I use the mouse to control a spacecraft like in this game?
The game I'm talking about is here: https://www.youtube.com/watch?v=7On9wuNJsjU
I'm still fairly new to programming, so it's very difficult for me so far. I've looked through a few posts on Answers and on the forums, but nothing is quite like how the plane is controlled in that game. How is it done?
(I would prefer C#, but javascript's good too.)
Answer by tommy1235 · Mar 22, 2014 at 04:13 AM
the best way I can think of doing this is to look at the mouse using Camera.ScreenToWorldPoint
this is the way I did it
public float TurningSpeed = 100;
Vector3 LookAtPos;
Vector3 SmoothedLookAtPos;
public float ForwardSpeed = 100;
void Update ()
{
//Look At the Mouse
LookAtPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 100));
SmoothedLookAtPos = Vector3.Lerp(SmoothedLookAtPos, LookAtPos, Time.deltaTime * 5);
transform.LookAt(SmoothedLookAtPos);
//Move forward (you could use a Rigidbody)
transform.position += transform.forward * Time.deltaTime * ForwardSpeed;
}
Thanks! This works pretty well, but I'm having some concerns now. As the x rotation approaches 90 or 270 (straight up or straight down), the rate of y rotation seems to increase dramatically. I played the game in the Youtube video, and the rate of turning is very linear, ie it doesn't move faster as x rotation changes.
By the way, I noticed some vibrations in the camera when I used your code. Putting it under FixedUpdate seemed to eli$$anonymous$$ate the problem, though I'm unsure why.
Your answer
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
How could I use the mouse as input for a flight control system (and space bar for thrust!) 2 Answers
Cannot control gameobject and play animation simultaneously 0 Answers
3D spaceship control using mouse? 0 Answers
force too -z ax 1 Answer