im quite new to unity so i guess this is an easy to solve problem i’m currently confronted with.
I want to control the rotation of a Rigidbody by moving the mouse. The rigidbody should always “Look At” the mouse.
Well, this is what i have, and it “seems” to work…sort of.
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 100)) {
var destPoint = hit.point;
var target = Vector3(destPoint.x, transform.position.y, destPoint.z);
transform.LookAt(target);
}
The problem is, that the rigidbody behaves a bit “jumpy” or “studdering”. Sometimes it’s reacting normal the other minute it’s not… :evil:
Your code doesn’t seem like it’s going to have the object always look at the mouse, it would only seem to do so when the mouse is over something within the specified range. And even then it won’t look at where the mouse is but some other position (the point of contact of your ray and some object, minus the fact that you’re replacing the y-position completely).
Is the functional goal here to have this object always look at the mouse? Sometimes look at the mouse? Only when it’s over something? other?
well, the goal is to make an object react to the mouse position, by rotating himself. And this should happen all the time - think of a topview tank game, where you control the movement of the object with your keyboard and aim with your mouse. Like in the 2D tutorial from Unity i’m ‘ignoring’ one axis completely (the depth - in my case the y-axis), so i thought i had to do this through the whole game programming … this is why i didn’t setup y for raytracing.
This will make the rigidbody rotate based on the current position of the mouse on screen. It’ll be at zero in the center, go to -180 on the left, and go to 180 on the right.
Also, if you increased the distance shot from your raycast, had the camera looking directly downwards, and had the tank in the center of the screen, your technique would also work fine. You just need to keep in mind that if your camera is 100 in the air, all the slants would be at a distance greater than 100.
I have a similar request to this, and have tried out your script. I need it to follow the mouse around in a complete 360 circle, however the current script seems to get stuck and not go all the way around.
I am trying to create a mouse drag rotatable wheel.
Any thoughts would be appreciated…
I know it’s been 10 years, but here’s my solution:
In words:
get the mouse position in screen coordinates (pixels)
subtract 0.5 screen width from x
subtract 0.5 screen height from y
(3 & 4 “move” the 0,0 point of the screen from the bottom left to the middle)
Take the arc tangent of y/x, this is your rotation angle in radians
switch to degrees; set this angle as your rotation angle
Notes:
A. you want to use mouse position, rather than mouse delta (I think)
B. you want to set the position each frame, without incrementing the value
C. cache the halfScreenWidth and halfScreenHeight, rather than recalculate each frame
// I'm using the new Input System
var mouseControl = Mouse.current.position;
Vector2 screenCoordinate;
screenCoordinate.x = mouseControl.x.ReadValue();
screenCoordinate.y = mouseControl.y.ReadValue();
screenCoordinate.x -= Screen.width * 0.5f;
screenCoordinate.y -= Screen.height * 0.5f;
// put y first, then x; switch to degrees
float angle = Mathf.Atan2(screenCoordinate.y, screenCoordinate.x) * Mathf.Rad2Deg;
// set rotation
var rotation = rb.rotation.eulerAngles;
rotation.z = angle;
rb.rotation = Quaternion.Euler(rotation);