szymski

Untitled

Oct 5th, 2019
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class BallMovement : MonoBehaviour
  6. {
  7. [SerializeField]
  8. private float _speed = 3f;
  9.  
  10. [SerializeField]
  11. private float _ballRadius = 1f;
  12.  
  13. private float _ballPerimeter;
  14.  
  15. void Start()
  16. {
  17. _ballPerimeter = 2f * Mathf.PI * _ballRadius;
  18. }
  19.  
  20. void OnDrawGizmos()
  21. {
  22. Gizmos.color = Color.yellow;
  23. Gizmos.DrawWireSphere(transform.position, _ballRadius);
  24. }
  25.  
  26. void Update()
  27. {
  28. var input = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
  29. var acceleration = input * _speed;
  30.  
  31. var toAdd = acceleration * Time.deltaTime;
  32.  
  33. transform.position += toAdd;
  34.  
  35. var horizontalDistTraveled = toAdd.x;
  36. var angle = horizontalDistTraveled / _ballPerimeter * 360f;
  37.  
  38. transform.Rotate(Vector3.back, angle);
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment