Skip to content

Instantly share code, notes, and snippets.

@pirkla
Last active October 6, 2019 15:06
Show Gist options
  • Save pirkla/083bcebe7412798f3792ba398e0efb07 to your computer and use it in GitHub Desktop.
Save pirkla/083bcebe7412798f3792ba398e0efb07 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class VelocityMove : MonoBehaviour
{
[SerializeField]
private float moveSpeed = 2500f;
[SerializeField]
private float turnSpeed = 90f;
[SerializeField]
private float jumpSpeed = 2000f;
private Rigidbody rb;
/// <summary>
/// Start is called on the frame when a script is enabled just before
/// any of the Update methods is called the first time.
/// </summary>
void Start()
{
rb = GetComponent<Rigidbody>();
}
/// <summary>
/// This function is called every fixed framerate frame, if the MonoBehaviour is enabled.
/// </summary>
void FixedUpdate()
{
if (Input.GetKey(KeyCode.Q))
{
Quaternion deltaRotation = Quaternion.Euler(-Vector3.up * turnSpeed * Time.deltaTime);
rb.MoveRotation(transform.rotation * deltaRotation);
}
if (Input.GetKey(KeyCode.E))
{
Quaternion deltaRotation = Quaternion.Euler(Vector3.up * turnSpeed * Time.deltaTime);
rb.MoveRotation(transform.rotation * deltaRotation);
}
if (Input.GetKey(KeyCode.W))
rb.AddForce(transform.forward * moveSpeed * Time.deltaTime);
if (Input.GetKey(KeyCode.S))
rb.AddForce(-transform.forward * moveSpeed * Time.deltaTime);
if (Input.GetKey(KeyCode.D))
rb.AddForce(transform.right * moveSpeed * Time.deltaTime);
if (Input.GetKey(KeyCode.A))
rb.AddForce(-transform.right * moveSpeed * Time.deltaTime);
if (Input.GetKey(KeyCode.Space))
rb.AddForce(transform.up * jumpSpeed * Time.deltaTime);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment