0
\$\begingroup\$

I have problems with a character. I want to move myself at some speed, and by typing on WASD keys to rotate this object and move to the side where the face is. The object moves continuously without pressing the key. Let's say something to the snake game.This is my code, it goes, but I can not do this rotation.

private float speed = 0.01f;
private float moveSpeed = 12f;
Vector2 vector = Vector2.up;
Vector2 moveVector;
[HideInInspector] public bool IsMoving = false;
[HideInInspector] public bool canPress = true;
 private void Start()
{    
    InvokeRepeating("Movement", 0.5f, speed);
}
   void Update()
{
    if (canPress)
    {
        if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
        {
            canPress = false;
            IsMoving = true;
            vector = Vector2.right;
            //transform.Rotate(0f, 0f, -90f);
            transform.rotation = Quaternion.Euler(transform.rotation.x, transform.rotation.y + rotateObj, transform.rotation.z);
          }
          else if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W))
          {
                canPress = false;
                IsMoving = true;
                vector = Vector2.up;
          }
          else if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S))
          {
                canPress = false;
                IsMoving = true;
                vector = -Vector2.up;
               //transform.Rotate(0f, 0f, -90f);
          }
          else if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
          {
                canPress = false;
                IsMoving = true;
                vector = -Vector2.right;
                //transform.Rotate(0f, 0f, +90f);
          }
      }
      moveVector = vector / moveSpeed;
    }
    void Movement()
    {
        if (IsMoving)
           transform.Translate(moveVector);
    }

If anyone knows how to do this I would be grateful. !!!

\$\endgroup\$
1
\$\begingroup\$
public GameObject shipSprite;
private float speed = 0.01f;
private float moveSpeed = 12f;
Vector2 vector = Vector2.up;
Vector2 moveVector;
[HideInInspector] public bool IsMoving = false;
[HideInInspector] public bool canPress = true;
private void Start()
{
    gameObject.GetOrAddComponent<ColliderEventSystem>().TriggerEntered += SpaceShiptTriggerEnter;
    InvokeRepeating("Movement", 0.5f, speed);

}
    void Update()
{
    if (canPress)
    {
        if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
        {
            canPress = false;
            IsMoving = true;
            vector = Vector2.right;
            shipSprite.transform.Rotate(0f, 0f, -90f);
        }
        else if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W))
        {
            canPress = false;
            IsMoving = true;
            vector = Vector2.up;
            shipSprite.transform.rotation = Quaternion.identity;
        }
        else if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S))
        {
            canPress = false;
            IsMoving = true;
            vector = -Vector2.up;
            shipSprite.transform.rotation = Quaternion.identity;
        }
        else if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
        {
            canPress = false;
            IsMoving = true;
            vector = -Vector2.right;
            shipSprite.transform.Rotate(0f, 0f, 90f);
        }
    }
    moveVector = vector / moveSpeed;
}
void Movement()
{
    if (IsMoving)
        transform.Translate(moveVector);
}
\$\endgroup\$
  • \$\begingroup\$ What did we do? ... we created a new object, and we put the ship into it, which contains just the sprite. In principle it works, but again with some disturbances.So I do not know what the problem is ... :/ \$\endgroup\$ – Gheorghe Ciusca Dec 5 '17 at 12:59
0
\$\begingroup\$

To Rotate your character at some specific point use this line first declare public float variable rotateObj = 90; and assign value.

 transform.rotation = Quaternion.Euler(transform.rotation.x, transform.rotation.y + rotateObj, transform.rotation.z);

To move your Character use this line public float speed;

private Rigidbody rb;

void Start ()
{
    rb = GetComponent<Rigidbody>();
}

 Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);

    rb.AddForce (movement * speed);
\$\endgroup\$
  • \$\begingroup\$ I do not think this is an answer . Maybe you did not understand my question )). let's say I play and the ship starts to move upwards, so if I key D, the ship turns to the right and starts to move to the right, I press D again, the ship turns to the right and moves again. And it does not matter whether you type D or W or A or S, the ship is moving in the indicated direction. \$\endgroup\$ – Gheorghe Ciusca Dec 5 '17 at 12:54
  • \$\begingroup\$ My script works, but not entirely, there are some errors, when I want to type S. \$\endgroup\$ – Gheorghe Ciusca Dec 5 '17 at 12:56

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.