- Home /
rotation based on mouse position
OK, so what im working on is a basic dog fighter like game. I wan the object to face the crosshairs at all times (set to mouse position in another script) i thought the best way to do this was to set the rotation based on the mouse position as well but i cant seem to get the calculations for the rotation right because it calls the left side of the screen 0 not the center. Can someone set me on the right path?
here is my code idk if you'll need it or if it will even help,
#pragma strict
//move variables
var leftThrust: ParticleSystem;
var rightThrust: ParticleSystem;
var centerThrust: ParticleSystem;
var speed: float=1;
var turn: float=1;
//rotate variables
var mouse_pos: Vector3;
var obeject_pos: Vector3;
var target: Transform;
var angle: float;
function Start () {
}
function Update ()
{
BasicMovement ();//begins on line 27
Turning ();// begins on line 88
}
function BasicMovement ()//basic movement Input
{
//zero out
if (Input.GetAxis("Horizontal")==0)
{
leftThrust.particleSystem.enableEmission=false;
rightThrust.particleSystem.enableEmission=false;
}
if (Input.GetAxis("Vertical")==0)
{
centerThrust.particleSystem.enableEmission=false;
}
if (Input.GetAxis("Horizontal")==0 && Input.GetAxis("Vertical")==0 )
{
rightThrust.particleSystem.enableEmission=false;
centerThrust.particleSystem.enableEmission=false;
}
//going left
if (Input.GetAxis("Horizontal")<0)
{
rightThrust.particleSystem.enableEmission=true;
transform.Rotate(0,0,-turn);
}
if (Input.GetAxis("Horizontal")<0 &&Input.GetAxis("Vertical")>0 )
{
rightThrust.particleSystem.enableEmission=true;
centerThrust.particleSystem.enableEmission=true;
}
//going Right
if (Input.GetAxis("Horizontal")>0)
{
leftThrust.particleSystem.enableEmission=true;
transform.Rotate(0,0,turn);
}
if (Input.GetAxis("Horizontal")<0 &&Input.GetAxis("Vertical")>0)
{
rightThrust.particleSystem.enableEmission=true;
centerThrust.particleSystem.enableEmission=true;
}
//going foward
if (Input.GetAxis("Vertical")>0)
{
centerThrust.particleSystem.enableEmission=true;
transform.Translate(-speed, 0, Time.deltaTime);
}
//going back
if (Input.GetAxis("Vertical")<0)
{
centerThrust.particleSystem.enableEmission=false;
transform.Translate(speed, 0, Time.deltaTime);
}
//going Up
if (Input.GetAxis("Jump")>0)
{
transform.Translate(0, Time.deltaTime,speed);
}
//going Down
if (Input.GetAxis("Jump")<0)
{
transform.Translate(0, Time.deltaTime,-speed);
}
}
function Turning ()
{
var angleX: float;
mouse_pos = Input.mousePosition;
mouse_pos.x = mouse_pos.x - obeject_pos.x;
mouse_pos.y = mouse_pos.y - obeject_pos.y;
angle = Mathf.Atan2( mouse_pos.y, mouse_pos.x)* Mathf.Rad2Deg;
angleX = angle + 90;
if ( angle < 90 && angle > -90)
{
transform.rotation=Quaternion.Euler(Vector2(-angleX,0));
}
}
ok i simplified my code a lot and i have it working well atleast better then it was anyways.
function Turning () { mouse_pos = Input.mousePosition; if ( angle < 90 && angle > -90) { transform.rotation=Quaternion.Euler(Vector3(270,mouse_pos.x,mouse_pos.y)); } }
my current problem is that i want to keep x at 270 (bc at 0 my asset is sideways for some reason) but when i set it as a static value the other axes respond by only rotating around the y axis but if i set it to something like mouse_pos.z the thing works perfect except my asset is sideways
srry that didnt format my code,
function Turning ()
{
mouse_pos = Input.mousePosition;
if ( angle < 90 && angle > -90)
{
transform.rotation=Quaternion.Euler(Vector3(270,mouse_pos.x,mouse_pos.y));
}
}
Answer by Lumos · Jun 01, 2013 at 03:27 PM
That's some complicated trigonometry you're doing there, and I'm not quite sure what it does. The screen's center you can get by using a Vector2 screenCenter = new Vector2(Screen.width / 2, Screen.height / 2);
Also, here's some simpler code that could probably help you out; it's what I'm using to control my spaceship.
public float rotationSpeed = 60.0f;
// Rotation around the Euler angles
float pitch = -Input.GetAxis("Mouse Y") * rotationSpeed * Time.deltaTime;
float yaw = Input.GetAxis("Horizontal") * rotationSpeed * Time.deltaTime;
float roll = -Input.GetAxis("Mouse X") * rotationSpeed * Time.deltaTime;
Vector3 rotation = new Vector3(pitch, yaw, roll);
rotation = Vector3.Lerp (Vector3.zero, rotation, Time.deltaTime * rotationSpeed); // Smooth...
// PHYSICS - nah
//Vector3 rotation = new Vector3(roll, yaw, pitch); // rigidbody rotation wants the angles in a different order...?
//transform.rigidbody.AddTorque(rotation);
transform.Rotate(rotation);
I'm achieving nice, smooth rotation around all Euler angles easily. Adding torque didn't really work, because it made the player compensate for the force by adding a counter-force in the opposite direction, and that never was too much of a nuisance to deal with, but it's also a way to do it if you so desire.
I hope this helps!
Answer by robertbu · Jun 01, 2013 at 03:05 PM
The code that initialized 'obeject_pos' is not here, but I'm going to assume it is set to a coordinate in world space. I'm guessing that it is transform.position. The issue you have is that Input.mousePosition is a Screen coordinate and your object lives in world coordinates. Before you can get the angle, you have to either convert the mouse to world coordinates, or convert the world coordinate to screen coordinates:
var v3 = Camera.main.WorldToScreenPoint(object.pos);
As for your turning code, I'm not sure what is going on since you are rotating around the 'X' axis. I'd need to know more about your camera setup and why the 'X' axis before I can verify the code. But the above line should get your calculations in the same coordinate system.
yeah i wrote that before i thought about rotating by mouse i was going change that to translate so it would be a strafe like movement
the coordinate difference makes sense ill try looking it up to get a better understanding of how it works, im kinda new if you cant tell lol
thanks for the help
Here is a bit of a rewrite to your Turning() so that the Right side of the object follows the mouse:
#pragma strict
var target : Transform;
function Update() {
Turning();
}
function Turning () {
var angleX: float;
var v3 = Camera.main.WorldToScreenPoint(target.position);
v3 = Input.mousePosition - v3;
var angle = $$anonymous$$athf.Atan2( v3.y, v3.x)* $$anonymous$$athf.Rad2Deg;
transform.eulerAngles = Vector3(0.0,0.0,angle);
}
Your answer
Follow this Question
Related Questions
"Perfect" Free Rotation Method? (like Blender) 2 Answers
Rotate object following mouse movement. Object jumps/ flips 1 Answer
Vertical mouse input inverted when player is turned around 0 Answers
Rotate object with mouse 1 Answer
Rotation of Object on single axis in direction of the mouse position 0 Answers