1
\$\begingroup\$

I want to rotate a cube left and right with that:

using UnityEngine;

public class PlayerMovement : MonoBehaviour {

    private Vector3 offset;
    public GameObject player;
    public GameObject center;
    public GameObject Left;
    public GameObject Right;
    public int step = 9;
    bool input = true;
    public float speed = 0.01f;

    void Update()
    {
        if(input == true)
        {
            if (Input.GetKey("a"))
            {
                StartCoroutine("moveLeft");
                input = false;
            }        }
        if (input == true)
        {
            if (Input.GetKey("d"))
            {
                StartCoroutine("moveRight");
                input = false;
            }
        }
    }


    IEnumerator moveLeft()
    {
        for(int i = 0; i < (90/step); i++)
        {
            player.transform.RotateAround(Left.transform.position, Vector3.forward, step);
            yield return new WaitForSeconds(speed);
        }
        center.transform.position = player.transform.position;
        input = true;
    }

    IEnumerator moveRight()
    {
        for (int i = 0; i < (90 / step); i++)
        {
            player.transform.RotateAround(Right.transform.position, Vector3.back, step);
            yield return new WaitForSeconds(speed);

        }
        center.transform.position = player.transform.position;

        input = true;
    }
}

and the problem is when I press a and d cube move left or right but + 0.5 up.


I also have tried this update method, but now it turns only on the y rotation axis:

public float rotationSpeed = 10f;
public GameObject player;

void Update()
{
    if(Input.GetAxis("Horizontal") < 0)
    {
        player.transform.Rotate(
            0, 
            Input.GetAxis("Horizontal") * rotationSpeed * Time.deltaTime, 
            0);
    }

    if (Input.GetAxis("Horizontal") > 0)
    {
        player.transform.Rotate(
            0, 
            Input.GetAxis("Horizontal") * rotationSpeed * Time.deltaTime, 
            0);
    }
}
\$\endgroup\$
1
\$\begingroup\$

I've done something similar. As isammour mentioned, use Input.GetAxis. As for why it's 0.5 up, that has to do with your RotateAround point of rotation. The easy way to solve it would be:

player.transform.RotateAround(Right.transform.position - new Vector3(0, 0.5, 0), Vector3.back, step);

or to work with other cube sizes:

player.transform.RotateAround(Right.transform.position - transform.localScale.y / 2, Vector3.back, step);

If you want to use the orientation of the phone to determine the input, I recommend you start here: https://unity3d.com/learn/tutorials/topics/mobile-touch/accelerometer-input or perhaps ask another question.

Edit:
full code for what I did, but I'm a fairly novice programmer and recommend you don't copy this in its entirety, it's probably quite flawed.

public float rotateSpeed;
private Vector3 turnPoint;
private float degX;
private float degZ;
private float direction;

private void Start()
{
    degX = 0; // As far as I know, it defaults to 0 without declaration, so this 2 lines aren't necessary
    degZ = 0;
}

private void FixedUpdate()
{
    Turn("Horizontal", 1, 0, Vector3.back, ref degX);
    Turn("Vertical", 0, 1, Vector3.right, ref degZ);
}

private void Turn(string button, int x, int z, Vector3 axis, ref float deg)
{
    if (Input.GetButton(button) && degX == 0 && degZ == 0) // So that it only turns in one direction at once
    {
        direction = Input.GetAxisRaw(button); // Direction needs to be stored for future use
        turnPoint = transform.position + new Vector3(0.5f * direction * x, -0.5f, 0.5f * direction * z); // Location of edge
        transform.RotateAround(turnPoint, axis, rotateSpeed * direction); // Turn around the edge
        deg += rotateSpeed * direction; // How much it has turned so far
    }
    if (deg != 0) // If the full rotation has not yet been made, repeat
    {
        transform.RotateAround(turnPoint, axis, rotateSpeed * direction);
        deg += rotateSpeed * direction;
        if (deg % 90 == 0) // % is a failsafe, deg should ideally be 90 or -90
        {
            deg = 0;
        }
    }
}

The way I've done it has it as a method. For now we can just ignore different scale cubes. The parameter button is either "Vertical" or "Horizontal", for now since you're working in 2 dimensions, you can just use "Horizontal" (or the accelerometer input, which I'm not sure how to use). Parameters x and z are used to locate the edge of the cube, but I feel like there's a better way of using this. Axis is just to determine direction of rotation.

\$\endgroup\$
  • \$\begingroup\$ Can you make the code , i don't understand .. please :) \$\endgroup\$ – Mihai Jun 11 '18 at 18:38
  • \$\begingroup\$ Ok, but it's going to look a little bit different from what you have. \$\endgroup\$ – Devintage Jun 11 '18 at 19:45
0
\$\begingroup\$

You can achieve this without using Couroutines, for example in your case you want to rotate left or right according to the player input.

So you should use Input.GetAxis()

if(Input.GetAxis("Horizontal") != 0)
{
    player.transform.Rotate(0,Input.GetAxis("Horizontal") * rotationSpeed * Time.deltaTime,0);
    player.transform.Translate(Vector3.forward * movementSpeed * Time.deltaTime);
}

Note that the value of Input.GetAxis() will be in the range -1...1

<0 is for "a" button

>0 is for "d" button

=0 When no input

\$\endgroup\$
  • \$\begingroup\$ This is a good alternative, don't need to see if Input.GetAxis("Horizontal") is negative or not, not equal to 0 is sufficient enough \$\endgroup\$ – Chad Jun 11 '18 at 19:25
  • \$\begingroup\$ Yes , It work but now I don't know how to move the object left right when is in rotation \$\endgroup\$ – Mihai Jun 11 '18 at 19:28
  • \$\begingroup\$ What do you mean by "is in rotation:" can you clarify more ? \$\endgroup\$ – isammour Jun 11 '18 at 20:40
  • \$\begingroup\$ Yes , sorry for my bad english :)) when i press a , for example , cube rotate and move to left \$\endgroup\$ – Mihai Jun 12 '18 at 6:37
  • \$\begingroup\$ then you don't need to move left and right, you just rotate then move forward Check the edited answer with additional line of code \$\endgroup\$ – isammour Jun 12 '18 at 8:32

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.