- Home /
How can I make my airplane rotate by my cursor position?
I am working on a Aviation Controller. I was wondering how to make it to where if my cursor center location is the screen width / 2, and screen height / 2. So that the center position is the center of the users screen. The farther the cursor is from that center position the more the aircraft will change its rotation. Here is a picture example:
The left is a diagram of what I am saying. The right is the players screen and how it would look to them.
How would I go about doing this? I can't get much further than,
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Here is quick script to get you started. It does not do exactly what you asked for, but, if attached to the plane, will allow you to rotate the plane by clicking the mouse and moving the cursor. Note it could be attached to an object in the center of the screen with the renderer turned off and the plane could mirror the rotation.
private var factor : float = 0.6;
private var v3StartPos : Vector3;
private var v3StartRot : Vector3;
private var v3Axes : Vector3 = new Vector3(1,1,0);
function On$$anonymous$$ouseDown(){
v3StartPos = Input.mousePosition;
v3StartRot = transform.eulerAngles;
}
function On$$anonymous$$ouseDrag(){
var v3T : Vector3 = Input.mousePosition;
var v3T2 : Vector3 = v3StartRot;
v3T2.y += (v3StartPos.x - v3T.x) * factor;
v3T2.x -= (v3StartPos.y - v3T.y)* factor;
transform.eulerAngles = v3T2;
}
A solution that did not require an object with a collider would take a somewhat different approach.
Answer by Mons1999 · Jan 24, 2013 at 09:18 PM
You just need to add a "Mouse Look" component that you can find in the imported assets in Compenent => Camera-Control => Mouse Look. Then if you move your cursor your plane will rotate perfectly. If you also want it to move in the same time, also add a "Character Controller" that you can find in Component => Physics => Character Controller, a "Character Motor" and a "FPS Input Controller" in Component => Character. I wish this answer helps you ;)
Your answer

Follow this Question
Related Questions
Camera rotation around player while following. 6 Answers
Retrieve the alpha of game objects texture at cursor position 1 Answer
Cannot rotate GameObject from C# Animator attached 1 Answer
Rotate GameObject Issue 1 Answer
Scrolling Sprite Object 0 Answers