Skip to content

Instantly share code, notes, and snippets.

@niiicolai
Last active June 27, 2020 02:12
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 niiicolai/46c09c018e270f396bf37793b3fede88 to your computer and use it in GitHub Desktop.
Save niiicolai/46c09c018e270f396bf37793b3fede88 to your computer and use it in GitHub Desktop.
using UnityEngine;
public class Movement : MonoBehaviour
{
private Rigidbody rigidbody;
[SerializeField]
private float speed;
[SerializeField]
private ForceMode forceMode;
// Start is called before the first frame update
void Start()
{
rigidbody = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
// EXAMPLE 1
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Vector3 velocity = new Vector3(0, 0, v);
velocity *= speed;
velocity -= rigidbody.velocity;
rigidbody.AddForce(velocity, forceMode);
/* EXAMPLE 2
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Vector3 velocity = new Vector3(0, 0, v);
velocity = transform.TransformDirection(velocity);
velocity *= speed;
velocity -= rigidbody.velocity;
rigidbody.AddForce(velocity, forceMode);
transform.Rotate(new Vector3(0, h, 0));
*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment