i want to rotate an object around its y axis so that it points to my mouse position. i tested so many different cases but i cant find a simple solution.
my best bet was this code:
Vector3 MouseWorldPosition = Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, transform.position.z));
transform.LookAt (MouseWorldPosition);
transform.rotation = Quaternion.Euler (new Vector3 (0, transform.rotation.eulerAngles.y, 0));
but its just completly wrong. i have big problems in understanding vector math
This should work, but only rotates 1 direction because I’m starting to learn how to write scripts myself. But when you attach this code to any object, and you move your mouse from left to right or from right to left, the object rotates around its Y-axis. Make the speedDamp value like 100 and you’ll see it moves.
var rotateMultiplier = 4;
var speedDamp : float = 1.0;
function Update() {
if (Input.GetAxis ("Mouse X"))
transform.Rotate(0, Time.deltaTime/rotateMultiplier*speedDamp, 0);
}
Ok I think I get it. But I don’t see why you can’t do it with transform.Rotate. Takes a bit more research I guess but it shouldn’t be too hard I think.
In order to get a object to look at the curser, you can cast a raycast from the camera onto the terrian. And have it look at the point where it hit.
The name of the object looking at the point is called ‘cube’ in my case but it can be changed to fit whatever the name is of the character. You can also change how far the raycast reaches by changing its distance (the distance is what 300 is). You can attach this script to your camera.
function Update () {
var ray : Ray;
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit : RaycastHit;
if(Physics.Raycast(ray,hit,300)){
var cube = GameObject.Find("Cube");
cube.transform.LookAt(hit.point);
}
ok then zero out the x rotation and z rotation after the transform.LookAt command.
function Update () {
var ray : Ray;
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit : RaycastHit;
if(Physics.Raycast(ray,hit,300)){
var cube = GameObject.Find("Cube");
cube.transform.LookAt(hit.point);
cube.transform.rotation.z = 0;
cube.transform.rotation.x = 0;
}
}
You may want something more smoother than transform.LookAt. Then you would use another method to make the object turn smoothly around.
var turnspeed = 3;
function Update () {
var ray : Ray;
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit : RaycastHit;
if(Physics.Raycast(ray,hit,300)){
var cube = GameObject.Find("Cube");
//this sets the direction of where we want to look. Subtracting the objects position of what we want to look
//at with the object we want facing it creates a direction. See lookRotation in scripting reference for more info.
var playerRotatedirection = Quaternion.LookRotation(hit.point - cube.transform.position);
//this tells the cube to actually move from its transform.rotation and face the direction we want it to face.
cube.transform.rotation = cube.transform.rotation = Quaternion.Lerp(cube.transform.rotation,playerRotatedirection,turnspeed*Time.deltaTime);
//zeros out the x and z rotations so it only turns on y
cube.transform.rotation.x = 0;
cube.transform.rotation.z = 0;
}
}