Created
February 7, 2018 03:47
-
-
Save dacharya64/119a775dc10d9f13919d1d7e37ec8282 to your computer and use it in GitHub Desktop.
Learning Unity: Player Movement
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class PlayerMovement : MonoBehaviour { | |
public float speed; //how fast the player moves | |
public float jumpForce; //how high the player jumps | |
//Private Vars | |
private Rigidbody rb; | |
// Use this for initialization | |
void Start () { | |
//get the rigidbody compoment of what this is attached to (ex. the player) | |
rb = GetComponent<Rigidbody>(); | |
} | |
// Update is called once per frame | |
void Update () { | |
if (Input.GetKeyDown(KeyCode.W)) { //JUMP | |
rb.velocity = transform.up * jumpForce; | |
} | |
if (Input.GetKey(KeyCode.A)) { //MOVE LEFT | |
transform.position += Vector3.left * speed * Time.deltaTime; | |
} | |
if (Input.GetKey(KeyCode.D)) { //MOVE RIGHT | |
// **How do you rewrite the 'move left' code for moving right?** | |
// Answer below | |
} | |
} | |
} | |
/* | |
* | |
* | |
* | |
* | |
* | |
* | |
* SOLUTION: transform.position += Vector3.right * speed * Time.deltaTime; | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment