Skip to content

Instantly share code, notes, and snippets.

@terrynoya
Forked from KRNKRS/PlayerMove.cs
Created July 20, 2017 15:09
Show Gist options
  • Save terrynoya/f7f54954f5802f29c063b86614797014 to your computer and use it in GitHub Desktop.
Save terrynoya/f7f54954f5802f29c063b86614797014 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class PlayerMove : MonoBehaviour {
private Rigidbody rb;
public float speed = 20;
public float QBPower = 120;
private Vector3 direction;
void Awake()
{
rb = this.GetComponent<Rigidbody>();
}
void FixedUpdate () {
var forward = this.transform.TransformDirection(Vector3.forward) * speed * Time.deltaTime;
var right = this.transform.TransformDirection(Vector3.right) * speed * Time.deltaTime;
if (Input.GetKey(KeyCode.W))
{
rb.AddForce(forward, ForceMode.VelocityChange);
direction = forward;
}
else if (Input.GetKey(KeyCode.S))
{
rb.AddForce(-forward, ForceMode.VelocityChange);
direction = -forward;
}
else
{
direction = forward;
}
if (Input.GetKey(KeyCode.D))
{
rb.AddForce(right, ForceMode.VelocityChange);
direction = right;
}
else if (Input.GetKey(KeyCode.A))
{
rb.AddForce(-right, ForceMode.VelocityChange);
direction = -right;
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
rb.AddForce(direction * QBPower, ForceMode.Impulse);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment