Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

wsad movement with either q and e rotate or right mouse

Discussion in 'Getting Started' started by KylonZarr, May 10, 2020.

  1. KylonZarr

    KylonZarr

    Joined:
    May 7, 2020
    Posts:
    1
    First off, im super new here, but having a blast and this forum has been SO helpful already. BUT I am just stumped on this, I am just not understanding what/how i need to write this into my script, Ive tried but only get errors.

    I have my w to move forward, s to move backward, a to move to the left and d to move to the right, BUT I cannot turn correctly or turn around at all, I want KotOR style controls, and i just want to add the q and e functions to my current script or have some way to have the mouse stay on the player, which i currently have, and control where the player is looking for he will walk/run in that direction

    Heres my current movement script. Please, im so stuck


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Movement : MonoBehaviour
    {

    public float movementSpeed;

    // Use this for initialization
    void Start()
    {

    }

    //Update is called once per frame
    void FixedUpdate()
    {

    if (Input.GetKey(KeyCode.LeftShift) && Input.GetKey("w"))
    {
    transform.position += transform.TransformDirection(Vector3.forward) * Time.deltaTime * movementSpeed * 2.5f;
    }
    else if (Input.GetKey("w") && !Input.GetKey(KeyCode.LeftShift))
    {
    transform.position += transform.TransformDirection(Vector3.forward) * Time.deltaTime * movementSpeed;
    }
    else if (Input.GetKey("s"))
    {
    transform.position -= transform.TransformDirection(Vector3.forward) * Time.deltaTime * movementSpeed;
    }

    if (Input.GetKey("a") && !Input.GetKey("d"))
    {
    transform.position += transform.TransformDirection(Vector3.left) * Time.deltaTime * movementSpeed;
    }
    else if (Input.GetKey("d") && !Input.GetKey("a"))
    {
    transform.position -= transform.TransformDirection(Vector3.left) * Time.deltaTime * movementSpeed;
    }
    }
    }
     
  2. TheMadTomato1209

    TheMadTomato1209

    Joined:
    May 7, 2020
    Posts:
    18
    Sorry i just didnt get it. What do you mean is not working? Mouse look or ?
     
  3. ProntName

    ProntName

    Joined:
    Apr 27, 2020
    Posts:
    15
    I am new here too, but I would look up in the documentation something about how to rotate your player about the y axis. Probably “transform.rotate” and mess around with it it. Positive values will probably rotate the character right while negative values will rotate the character left. Also, maybe look up “Input.GetAxis” as that may be a cleaner way to do what you are trying to do on the WASD functionality.

    I’ve only been using Unity for about three weeks (brand spanking new to programming since the beginning of April), but one thing I’ve learned is that you want to develop your researching skill—that is the best way you will learn how to do something and it will stick in your brain all the more! And from what I have been reading, probably THE most important skill you will need throughout your entire programming career.

    Let me know if you ever want to kick around ideas too since I’m looking for people with a similar experience level to learn and grow with :)
     
  4. TheMadTomato1209

    TheMadTomato1209

    Joined:
    May 7, 2020
    Posts:
    18
    transform.rotate(x, y, z). Import the mouse input in the "y". Check out Brackeys on youtube, if you havent. His tutorials are awesome.