using System.Collections; using System.Collections.Generic; using UnityEngine; public class TestScript : MonoBehaviour { public Vector3 DesiredMovementDirection = new Vector3(5f, -3f, 0); // desired direction (this vector will be normalized) public float DesiredSpeed = 0.9f; float _distanceTraveled = 0; SpriteRenderer _mySpriteRenderer; void Start() { _mySpriteRenderer = GetComponent(); Debug.Log("Start position is " + transform.position + " with desired movement direction " + DesiredMovementDirection + " and speed " + DesiredSpeed + " WorldUnits per second"); } void Update() { // Make sprite face direction transform.rotation = Quaternion.Euler(0, 0, Mathf.Atan2(DesiredMovementDirection.y, DesiredMovementDirection.x) * 180f / Mathf.PI); _mySpriteRenderer.flipY = DesiredMovementDirection.x < 0; // When moving left the rotation puts our character upside down, so we need to fix that // Move game object var changeInPosition = DesiredMovementDirection.normalized * DesiredSpeed * Time.deltaTime; // the movement vector transform.position += changeInPosition; // move GameObject _distanceTraveled += changeInPosition.magnitude; // increment distance traveled by magnitude of movement vector } void OnApplicationQuit() { Debug.Log("Application quit after " + Time.time + " seconds"); Debug.Log("End position is: " + transform.position); Debug.Log("Distance traveled is " + _distanceTraveled + " WorldUnits "); Debug.Log("GameObject moved at " + (_distanceTraveled / Time.time) + " WorldUnits per second"); } }