Skip to content

Instantly share code, notes, and snippets.

@MattGen
Created November 29, 2021 20:25
Show Gist options
  • Save MattGen/327284c926ec459c387d007a28f8f916 to your computer and use it in GitHub Desktop.
Save MattGen/327284c926ec459c387d007a28f8f916 to your computer and use it in GitHub Desktop.
Rust-like mini copter code (Unity)
using System.Collections.Generic;
using UnityEngine;
public class CopterControl : MonoBehaviour
{
public float thrust;
public ForceMode forceMode;
public float rotSpeed;
void Update()
{
if(Input.GetKey(KeyCode.Z))
{
gameObject.GetComponent<Rigidbody>().AddRelativeForce(Vector3.up*thrust,forceMode);
}
if(Input.GetKey(KeyCode.S)){gameObject.GetComponent<Rigidbody>().AddRelativeForce(-Vector3.up*thrust,forceMode);}
if(Input.GetKey(KeyCode.Q)){gameObject.GetComponent<Rigidbody>().AddTorque(Vector3.up*-rotSpeed,forceMode);}
if(Input.GetKey(KeyCode.D)){gameObject.GetComponent<Rigidbody>().AddTorque(Vector3.up*rotSpeed,forceMode);}
gameObject.transform.Rotate(Vector3.right,Input.GetAxis("Mouse Y"), Space.Self);
gameObject.transform.Rotate(-Vector3.forward,Input.GetAxis("Mouse X"), Space.Self);
if(Input.GetButton("Fire1")){CopterFire();}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment