- Home /
how can i make a object look at the mouse click only in the y axis?
With my code, the objecto look at the mouse click in all axis, and i justo want him to look at the y axis.
Here is my code.
> Blockquote
public class lookat : MonoBehaviour {
private Vector3 mousePos;
void Update()
{
if (Input.GetMouseButton(0))
RotateToMouse();
}
void RotateToMouse()
{
Vector3 vector = Input.mousePosition;
vector.z = Mathf.Abs(Camera.main.transform.position.y - transform.position.y);
vector = Camera.main.ScreenToWorldPoint(vector);
transform.LookAt(vector);
}
}
Answer by jnbbender · Jul 19, 2017 at 05:14 PM
I would think it would just be a matter of saving off the Vector of your original transform and setting the new vector appropriately. Sorry, bad explanation.
I also think you mean to store off your Mathf.Abs to vector.y don't you?
void RotateToMouse()
{
Vector3 vector = Input.mousePosition;
vector.y = Mathf.Abs(Camera.main.transform.position.y - transform.position.y);
// originalVec is saved off when the head/position is moved (whatever).
// This is stored somewhere
vector.x = originalVec.x;
vector.z = originalVec.z;
vector = Camera.main.ScreenToWorldPoint(vector);
transform.LookAt(vector);
}
this originalVec is the position of the object that i want rotate?
@Caldas Yes. It's the object you want to make look at the mouse.
Your answer
Follow this Question
Related Questions
Trouble adding score when specific object is clicked. (C#) 1 Answer
Object facing the wrong way? 1 Answer
onmousedown on object teleport 1 Answer
Rotation with multiple objects 1 Answer
2d obj look at limit in unity 0 Answers