0
\$\begingroup\$

im beginner to unity so im using this code to rotate object with mouse with max rotation of 35 degrees,it is working but it's only possible to rotate object if mouse is under of that object. here is my code:

    using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class rotObj : MonoBehaviour {
    //private float baseAngle = 0.0f;
    private float maxRotaion = 35f;
    void Update () {
        var dir = Camera.main.ScreenToWorldPoint (Input.mousePosition) - transform.position;
        var angle = Mathf.Atan2 (dir.y, dir.x) * Mathf.Rad2Deg;
        if (angle >= -maxRotaion && angle <= maxRotaion)
            transform.localRotation = Quaternion.AngleAxis (angle, Vector3.forward);
    }
}

as i think, localrotation is making that problem? how can i fix this without removing maximum rotation angle and will be able to rotate object not just from mouse under object, but from any position? my english grammar is very bad but i hope you will understand

\$\endgroup\$
6
  • \$\begingroup\$ Where do you want the mouse to be instead of "under the object" ? \$\endgroup\$
    – DMGregory
    Commented Apr 6, 2018 at 13:21
  • \$\begingroup\$ actually im making that game for android, so it must be in upper position not under of it, it makes rotating "gun" very annoying \$\endgroup\$
    – Nikka
    Commented Apr 6, 2018 at 15:57
  • \$\begingroup\$ Hmm - could you include an image or diagram labeling the range of rotation, just to make sure we understand your intentions correctly? \$\endgroup\$
    – DMGregory
    Commented Apr 6, 2018 at 16:23
  • \$\begingroup\$ i.imgur.com/Ss1H5VK.png here is explanation of what it does, and what's the problem, also thanks for helping. \$\endgroup\$
    – Nikka
    Commented Apr 6, 2018 at 18:02
  • \$\begingroup\$ ...I'm even more confused now. I'm not sure how to interpret this diagram in terms of what you want the turret to do. \$\endgroup\$
    – DMGregory
    Commented Apr 6, 2018 at 20:06

1 Answer 1

0
\$\begingroup\$

The standard mathematical convention for the angles returned by atan2(y, x) is that they're measured counter-clockwise from the positive x-axis.

If I understand your problem correctly, you want to measure them from the positive y-axis instead, which you can do by rotating your coordinates 90 degrees clockwise (exchanging x & y and negating x):

var dir = Camera.main.ScreenToWorldPoint (Input.mousePosition) - transform.position;
var angle = Mathf.Atan2 (-dir.x, dir.y) * Mathf.Rad2Deg;

Or, for more flexibility, you could capture the direction your object is pointing at startup, and measure your angles from that direction. Then you can rotate your turret to any arbitrary angle and it will work, without needing different atan math for every combination:

Quaternion initialOrientation;

private void Start() {
    initialOrientation = transform.rotation;
}

void Update() {        
    var dir = Camera.main.ScreenToWorldPoint(Input.mousePosition + Vector3.forward) - transform.position;
    // Rotate the direction into our initial reference frame.
    dir = Quaternion.Inverse(initialOrientation) * dir;

    var angle = Mathf.Atan2(-dir.x, dir.y) * Mathf.Rad2Deg;
    angle = Mathf.Clamp(angle, -maxRotaion, maxRotaion);

    // Rotate the result back into worldspace.
    transform.rotation = initialOrientation * Quaternion.AngleAxis(angle, Vector3.forward);
}    
\$\endgroup\$
1
  • \$\begingroup\$ thank you very very very much for helping me, this has fixed my problem again thank you! \$\endgroup\$
    – Nikka
    Commented Apr 7, 2018 at 10:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .