0

I wanted to restrict the area of the arrow movement between -140 and -40 degrees as indicated in the drawing but as the distribution of the degrees in the angular area is strange if I limit it between -140 and -40 as a minimum and maximum respectively, it only lets me move the arrow in the red area and not in the desired one, I mean that I want to move the arrow only in the not red area, if someone can help me starting from this code without changing it much, I would be very grateful, thank you.

Edit: Added provisional code based on conditionals.

explanatory diagram

    Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
    difference.Normalize();
    float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
    //rotationZ = Mathf.Clamp(rotationZ, -140, 180);
    if (rotationZ <= -90 && rotationZ > -140) { rotationZ = -140; }
    if (rotationZ > -90 && rotationZ < -40) { rotationZ = -40; }
    transform.rotation = Quaternion.Euler(0f, 0f, rotationZ);
    
    Debug.Log(rotationZ);
    if (rotationZ < -90 || rotationZ > 90)
        {

        if (rotationZ <= -90 && rotationZ > -140) { rotationZ = -140; }
        if (rotationZ > -90 && rotationZ < -40) { rotationZ = -40; }
        if (myPlayer.transform.eulerAngles.y == 0)
            {

                transform.localRotation = Quaternion.Euler(180, 0, -rotationZ);


            }
            else if (myPlayer.transform.eulerAngles.y == 180)
            {
                transform.localRotation = Quaternion.Euler(180, 180, -rotationZ);
            }


        }
    }
}
  • Is it 3D game and feature is like looking around in Rust pressing Alt, so characted will not look 180 degrees behind? – Whitebrim Nov 18 '20 at 13:06
  • I don't like this code, but at least you can use ```if(rotationZ <= -90 && rotationZ > -140) {rotationZ = -140;} if(rotationZ > -90 && rotationZ < -40) {rotationZ = -40;} ``` – Whitebrim Nov 18 '20 at 13:19
  • @Whitebrim What would you make instead? I am interested in learning a more efficient way. I attach the complete code since there is also a part in which I change the rotation of the image so that it makes a mirror version. –  Nov 18 '20 at 13:59
  • 1
    change -140 to 220 – Mohamed Awad Nov 18 '20 at 13:59
  • @MohamedAwad Unity degrees goes from 0º to 180º and -180º to 0º for the opposite. –  Nov 18 '20 at 14:08
  • 1
    @Antoine yes it is, but it's a Circle degree equation, just a simple trick, Unity Convert it by add or subtract 360 degrees, that means 220 is equal to -140, this convert will make your arrow move from -140 to -40 in the nonred area. just try it rotationZ = Mathf.Clamp(rotationZ, -40, 220); – Mohamed Awad Nov 18 '20 at 14:27
  • oh, so you have edited your question, well, there is no need for all of this, this prev script will work perfectly, I guess. just edit the Clamp method "from -40 to 220 – Mohamed Awad Nov 18 '20 at 14:32
  • @MohamedAwad In this video I record what happens, apparently doesn't perform that conversion [video](https://www.youtube.com/watch?v=F7AVRknGdyE&feature=youtu.be) –  Nov 18 '20 at 14:57
  • as I saw from console logs the degree lies between -40 and 220, (the nonRed area as you mentioned) if you print the object rotation it will be around -140 to -180/180 to 0/-0 to -40, I have tested it out. – Mohamed Awad Nov 18 '20 at 15:04
  • @MohamedAwad Yes, that's why I told you that it doesn't do that conversion so Unity ignores 220 in the Mathf because the maximum value for rotationz is 180. So I still don't know how to put that limit without using that 4 conditionals because the provisional solution is a mess. –  Nov 18 '20 at 15:57
  • @Whitebrim No xd, I love Rust too –  Nov 18 '20 at 18:18
  • @Antoine wrote as answer – Whitebrim Nov 18 '20 at 20:18

2 Answers2

0
    void FixedUpdate()
    {
        
        Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) 
        - transform.position;
        difference.Normalize();
        float rotationZ = Mathf.Atan2(difference.y, difference.x) * 
        Mathf.Rad2Deg;

        if (rotationZ <= -90 && rotationZ > -140) { rotationZ = -140; }
        else if (rotationZ > -90 && rotationZ < -40) { rotationZ = -40; }

        transform.rotation = Quaternion.Euler(0f, 0f, rotationZ);

        if (rotationZ < -90 || rotationZ > 90)
        {
            if (rotationZ <= -90 && rotationZ > -140) { rotationZ = -140; }
            else if (rotationZ > -90 && rotationZ < -40) { rotationZ = -40; }

            if (myPlayer.transform.eulerAngles.y == 0)
            {
                    transform.localRotation = Quaternion.Euler(180, 0, - 
                    rotationZ);
            }
            else if (myPlayer.transform.eulerAngles.y == 180)
            {
                    transform.localRotation = Quaternion.Euler(180, 180, - 
                    rotationZ);
            }
        }
    }
}
0
private readonly float deadZone = Mathf.Sin(-40 * Mathf.Deg2Rad);
private Vector3 rightDeadZoneEnd = new Vector3(Mathf.Cos(-40 * Mathf.Deg2Rad), Mathf.Sin(-40 * Mathf.Deg2Rad), 0);
private Vector3 leftDeadZoneEnd = new Vector3(Mathf.Cos(-140 * Mathf.Deg2Rad), Mathf.Sin(-140 * Mathf.Deg2Rad), 0);

private void Update()
{
    Vector2 direction = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
    direction.Normalize();
        
    if (direction.y < deadZone)
    {
        if (direction.x >= 0)
        {
            direction = rightDeadZoneEnd;
        }
        else
        {
            direction = leftDeadZoneEnd;
        }
    }

    if (direction.x < 0)
    {
        transform.localScale = new Vector3(1, -1, 1);
    }
    else
    {
        transform.localScale = Vector3.one;
    }

    direction = Quaternion.Euler(0, 0, 90) * direction;

    transform.localRotation = Quaternion.LookRotation(Vector3.forward, direction);
}
Whitebrim
  • 396
  • 2
  • 9