Skip to content

Instantly share code, notes, and snippets.

@niiicolai

niiicolai/Movement.cs

Last active Jun 27, 2020
Embed
What would you like to do?
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
You can’t perform that action at this time.