Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

I am having an issue on how to move sideways

Discussion in 'Game Design' started by aabhiseka, Jul 3, 2019.

  1. aabhiseka

    aabhiseka

    Joined:
    Jul 3, 2019
    Posts:
    1
    My player cannot move sideways whenever I tick on the box. The following is my scripts:

    using UnityEngine;
    using System.Collections;

    public class PlayerMovement : MonoBehaviour
    {
    public float moveSpeed = 5.0f;
    public float rotateSpeed = 60.0f;
    public bool canMoveSideways = false;

    void Update()
    {
    Movement();
    }

    void Movement()
    {
    if (Input.GetKey(KeyCode.UpArrow))
    {
    Debug.Log("Key UpArrow Pressed.");

    this.transform.Translate (new Vector3(0, 0, moveSpeed * Time.deltaTime));
    }
    else if (Input.GetKey(KeyCode.DownArrow))
    {
    Debug.Log("Key DownArrow Pressed.");

    this.transform.Translate (new Vector3(0, 0, -moveSpeed * Time.deltaTime));
    }



    if (Input.GetKey(KeyCode.LeftArrow))
    {
    if (canMoveSideways)
    {
    Debug.Log("Key LeftArrow Pressed.");

    this.transform.Translate (new Vector3(-moveSpeed * Time.deltaTime, 0, 0));
    }
    else
    {
    Debug.Log("Key LeftArrow Pressed:Rotate");

    this.transform.Rotate (new Vector3(0, -rotateSpeed * Time.deltaTime, 0));
    }
    }
    else if (Input.GetKey(KeyCode.RightArrow))
    {
    if (canMoveSideways)
    {
    Debug.Log("Key RightArrow Pressed.");

    this.transform.Translate (new Vector3(moveSpeed * Time.deltaTime, 0, 0));
    }
    else
    {
    Debug.Log("Key RightArrow Pressed:Rotate");

    this.transform.Rotate (new Vector3(0, rotateSpeed * Time.deltaTime, 0));
    }
    }

    if (Input.GetKey(KeyCode.W))
    {
    Debug.Log("Key W Pressed.");

    this.transform.Translate (new Vector3(0, moveSpeed * Time.deltaTime, 0));
    }
    else if (Input.GetKey(KeyCode.S))
    {
    Debug.Log("Key S Pressed.");

    this.transform.Translate (new Vector3(0, -moveSpeed * Time.deltaTime, 0));
    }
    }
    }
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    8,191
    Welcome to the forums! To get a good answer to a question like this, please do two things: (1) use code tags (in the formatting toolbar), and (2) don't just say "I can't do it" or "it doesn't work"... instead describe exactly what happens when you try, and how this differs from what you expected.
     
    Vryken, TenKHoursDev, YBtheS and 2 others like this.
  3. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    4,489
    3; try to find the proper topic to ask questions, coding questions aren't good in the game design topic
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    6,515
    You should say what is happening, like if your Debug.Log messages you are using are outputing or not, and if the Debug.Log messages indicate the character should be sliding or rotating. As it is, I'm wondering if you are setting canMoveSideways back to false in some other script.
     
    Vryken, YBtheS and SparrowsNest like this.