Skip to content

Instantly share code, notes, and snippets.

@onionmk2
Created April 1, 2017 02:44
Show Gist options
  • Save onionmk2/c8285643a18a80f552a5c2fb5b9e8e31 to your computer and use it in GitHub Desktop.
Save onionmk2/c8285643a18a80f552a5c2fb5b9e8e31 to your computer and use it in GitHub Desktop.
Translate, MovePosition, Avelocity=, AddForce...
using System;
using System.Collections;
using UnityEngine;
class MovementTest : MonoBehaviour
{
private Rigidbody rigid;
void Start()
{
rigid = GetComponent<Rigidbody>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.T))
{
transform.Translate(Vector3.forward * 10);
return;
}
if (Input.GetKeyDown(KeyCode.M))
{
var position = rigid.position + Vector3.forward * 10;
rigid.MovePosition(position);
return;
}
if (Input.GetKeyDown(KeyCode.V))
{
rigid.velocity = Vector3.right * 10;
Action changeDirection = () => { rigid.velocity = Vector3.forward * 10; };
StartCoroutine(DelayExec(changeDirection));
return;
}
if (Input.GetKeyDown(KeyCode.A))
{
rigid.AddForce(Vector3.right * 500);
Action addForwardForce = () => { rigid.AddForce(Vector3.forward * 500); };
StartCoroutine(DelayExec(addForwardForce));
return;
}
}
private IEnumerator DelayExec(Action action)
{
yield return new WaitForSeconds(1);
action();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment