Hello, I want my character to always face the mouse position. This is a isometric type game.
This is how i’ve done it now, in the Update function:
//aiming
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray,hit,1000)) {
Debug.DrawLine (ray.origin, hit.point,Color.green);
aimPos = Vector3(hit.point.x,upperBody.position.y,hit.point.z);
}
upperBody.LookAt(aimPos);
This sort of works, however. The character will look at objects sticking up out of the ground, as the camera ray hit’s them. This results in the player not being able to look parallel to a wall for example, since they will either look AT the wall, or at the ground beside it.
I know that I can put the objects that I don’t want the ray to collide with on an IgnoreRayCast layer, however, that creates another problem. If the character climbs up on a wall, and that wall is on ignore raycast, if the cursor is put below the character, it will still look up (away from the camera), since the ray from the camera passes through the wall that the character is standing on, and lands in front of the character.
What I want is basically: Whenever the cursor is below the character, it is supposed to look down. When it is above the character, it is supposed to look up, same things goes for left and right.
Could anyone please point me in the right direction?
Thanks.
/Alex
EDIT: Below is a screenshot of the issue. As you can see, the mouse cursor is below the character, but it doesn’t look towards the mouse, since the ray shoots through the object that the character is standing on. The objects need to ignore the raycast, since I always want the character to look at the mouse position, not at the objects. (The legs rotate independently of the upper body, they are irrelevant to this issue)