Question by
mynameisjun · Dec 14, 2017 at 08:09 AM ·
anglemathftrigonometry
angle problem
hey guys now i want this cannon to aim up or down to where the mouse is, when the mouse is on the terrain, otherwise to stop. but it is just working totally wrong. here is the code.
void Update () {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 8)) // if ray hits the terrain
{
Vector3 localPoint = transform.InverseTransformPoint(hit.point);
if (localPoint.z > 0.0f)
{
curRot = Mathf.Atan2(localPoint.z, localPoint.y)*Mathf.Rad2Deg; // i think the problem is here
}
}
else // otherwise stop
{
curRot = 0.0f;
}
transform.Rotate(curRot * Time.deltaTime, 0.0f, 0.0f);
}
][1]
i tried once without mathf.rad2Deg, but didn't work properly too. hope someone could answer this problem. thanks
untitled.png
(443.5 kB)
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612144550im_/https://answers.unity.com/themes/thub/images/avi.jpg)