0
\$\begingroup\$

Currently I am working on a 2D game just like duet game, it is my first game (Unity 2D).

When I press left key both points rotate fine. But when I press right, the points are not rotated.

So my problem is why do both points rotated only in one direction

Code:

public class TouchControll : MonoBehaviour
{
  float movespeed = 3;
  float angle = 45;

  void Update()
  {
    if (Input.GetKey(KeyCode.LeftArrow))   // when i m press leftarrow both point should be rotate fine  
    {
        transform.Rotate(Vector3.forward * Time.deltaTime * Input.GetAxis("Horizontal") * angle * movespeed);
        Debug.Log("moveleft");
    }

    if (Input.GetKey(KeyCode.RightArrow))  // problem is here when i m press right arrow key both point are not rotated
    {
        transform.Rotate(Vector3.forward * Time.deltaTime * Input.GetAxis("Vertical") * angle * movespeed);
        Debug.Log("moveright");
    }
  }
}

Output:

create a empty gameobject: enter image description here first circle enter image description here second circle enter image description here

\$\endgroup\$
  • \$\begingroup\$ Please clarify which platform and input device you are using. \$\endgroup\$ – Theraot Jun 26 at 21:29
  • \$\begingroup\$ @Theraot i m making game for android ,i m edit my question and when i m press left key both point rotated fine but problem is here when i m press right then point are not rotated thanks for replay plzz help?? \$\endgroup\$ – jishan nuran Jun 27 at 16:37
0
\$\begingroup\$

You code does not seem right for Android.

  • Input.GetKey in intended to work with a physical keys (see How i can control button input android?). Instead you probably want to work with touch input. See Input.GetTouch.

  • I have no idea what you are mapping at Input.GetAxis("Horizontal") and Input.GetAxis("Vertical"), however I would expect to use a single axis for rotation in both directions. Perhaps Input.GetAxis("Vertical") is mapped to nothing and that is why it does not work.

If you want your second case to have rotation in the opposite direction to the what you have on the first case, I would suggest to have exactly the same code, but use -angle instead of angle.


Addendum: This is an example of what I mean:

void Update()
{
    if (Input.GetKey(KeyCode.LeftArrow))
    {
        transform.Rotate(Vector3.forward * Time.deltaTime * Input.GetAxis("Horizontal") * angle * movespeed);
        Debug.Log("moveleft");
    }
    if (Input.GetKey(KeyCode.RightArrow))
    {
        transform.Rotate(Vector3.forward * Time.deltaTime * Input.GetAxis("Horizontal") * -angle * movespeed);
        Debug.Log("moveright");
    }
 }

In this case both are on the "Horizontal" axis, which I am assuming you need there. However rotation on one direction uses angle and for the opposite direction it is -angle.

I want to reiterate that they are both using Input.GetAxis("Horizontal") in the example above.


Two other ideas:

  • It is common to have Input.GetAxis("Horizontal") mapped left and right keys. Which means that you would implement this with a single block:

    void Update()
    {
        if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.RightArrow))
        {
            transform.Rotate(Vector3.forward * Time.deltaTime * Input.GetAxis("Horizontal") * angle * movespeed);
        }
     }
    

    Why? Because it is the axis that handling the direction of the rotatiton.

  • Alternatively, you may not want to use axis at all (try and see if that works for you the way you want):

    void Update()
    {
        if (Input.GetKey(KeyCode.LeftArrow))
        {
            transform.Rotate(Vector3.forward * Time.deltaTime * angle * movespeed);
            Debug.Log("moveleft");
        }
        if (Input.GetKey(KeyCode.RightArrow))
        {
            transform.Rotate(Vector3.forward * Time.deltaTime * -angle * movespeed);
            Debug.Log("moveright");
        }
     }
    

It is also possible that what you need to do is to remap the axis you have. You can do that in the Input Manager, go to the menu Edit > Project Settings > Input. There you can find the defined axis and map them to what you want.

See also Beginner Unity Basics #14 - Input Manager.

\$\endgroup\$
  • \$\begingroup\$ thanks for suggestion but you saay:single axis for rotation in both directions but how ?? \$\endgroup\$ – jishan nuran Jun 27 at 17:15
  • \$\begingroup\$ plz help?? i m little bit mistake in my programme?? can u edit my code??plzz \$\endgroup\$ – jishan nuran Jun 27 at 17:28
  • \$\begingroup\$ @jishannuran No, I can't edit your code. \$\endgroup\$ – Theraot Jun 27 at 17:39
  • \$\begingroup\$ i m trying -angle. but still not solved my problem \$\endgroup\$ – jishan nuran Jun 27 at 18:02
  • \$\begingroup\$ @jishannuran expanded answer. \$\endgroup\$ – Theraot Jun 27 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.