Hello. How can I convert something that uses EularAngles for individual axes to something that uses Transform.rotation?
// get the relative and world position of the mouse.
var worldMouse=ray.origin+ray.direction * dist;
var relativeMouse=transform.InverseTransformPoint(worldMouse);
//assign rotations based on the relative mouse position
transform.localEulerAngles.x=-relativeMouse.y* 10;
transform.localEulerAngles.y= relativeMouse.x* 10;
transform.localEulerAngles.z=-relativeMouse.x* 1;
Thanks
transform.localRotation = Quaternion.Euler(x,y,z);
?
would it be something like this?
transform.localRotation = Quaternion.Euler(-relativeMouse.y* 10,relativeMouse.x* 10,-relativeMouse.x* 1);
I get no errors when I use that, and the object still rotates to look at the mouse, but I am still having a problem.When the cursor moves outside of a certain radius from where the ship is on the screen, the ship’s rotation gets kinda crazy (it starts quickly rotating in a bunch of random directions). I was told that this is because I am using transform.Euler angles, and if I change to to transform.rotation this would not happen.
I you write something like this:
transform.rotation = Quaternion.Euler(...);
Then you’re still using Euler angles.
What would have to be modified to omit Euler angles?
I cant really answer that question without knowing what exactly you are trying to achieve. Euler angles can be avoided in most scenarios, but I cant tell from your code what you want to happen.
The script an object to look at the mouse cursor without introducing any roll into the objects rotation. The complete script also allows an object’s position on the screen to be controlled by the mouse cursor(this is for a starfox style game).
static var speed=0.0;
static var UDLRspeed=1.5;
static var camDistance=4.5;
var cam : Transform;
function Start(){
transform.position=cam.position + cam.forward * camDistance;
}
function Update(){
// find the point where we have our mouse in relation to our ship.
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var plane : Plane=new Plane(Camera.main.transform.forward, transform.position + transform.forward);
var dist : float=0.0;
plane.Raycast(ray, dist);
// get the relative and world position of the mouse.
var worldMouse=ray.origin+ray.direction * dist;
var relativeMouse=transform.InverseTransformPoint(worldMouse);
//assign rotations based on the relative mouse position
//transform.localEulerAngles.x=-relativeMouse.y* 10;
//transform.localEulerAngles.y= relativeMouse.x* 10;
//transform.localEulerAngles.z=-relativeMouse.x* 1;
// do world move of the player towards the world position of the mouse
transform.position=Vector3.Lerp(transform.position, worldMouse, UDLRspeed * Time.deltaTime);
// calculate the speed based off of if we have the left shift key down
var sp=speed;
if(Input.GetKey(KeyCode.LeftShift))sp=sp * 5;
// update the position of the camera and player based off wher the camera is looking.
cam.position+=cam.forward * sp;
var camoffset=cam.InverseTransformPoint(transform.position);
camoffset.z=camDistance;
transform.position=cam.TransformPoint(camoffset);
}
Your problem doesn’t have to do with Euler angles… but rather the fact that you are basing your rotation off of a local position on your transform which changes as the object rotates. You have basically created a circular reference, which is why your object is going crazy.
What you want is to find your relativeMouse variable like this:
var relativeMouse= cam.WorldToScreenPoint(transform.position) - Input.mousePosition;
now this value will be in pixels so you will want to maybe divide it by 10 or something to get a reasonable rotation.
I have not tested your code out (away from my computer at the moment), but if it is in pixels, wouldnt the rotation be different depending on the users screen resolution?
Yes. Of course you can normalize the values by just dividing by the width of the screen.