Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@dacharya64
Created February 7, 2018 03:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacharya64/119a775dc10d9f13919d1d7e37ec8282 to your computer and use it in GitHub Desktop.
Save dacharya64/119a775dc10d9f13919d1d7e37ec8282 to your computer and use it in GitHub Desktop.
Learning Unity: Player Movement
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