Advertisement
KodingKid

C# Unity3D Ball Physics

Apr 5th, 2021
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.71 KB | None | 0 0
  1. using UnityEngine;
  2. public class Player : MonoBehaviour { }
  3. void Update () {
  4.         Vector2 playerControls;
  5.         playerInput.x = 0f;
  6.         playerInput.y = 0f;
  7.         transform.localPosition = new Vector3(playerInput.x, 1f, playerInput.y);
  8.     playerInput.x = Input.GetAxis("Horizontal");
  9.     playerInput.y = Input.GetAxis("Vertical");
  10.     playerInput = Vector2.ClampMagnitude(playerInput, 2f);
  11.     Vector3 velocity = new Vector3(playerInput.x, 0f, playerInput.y);
  12.         Vector3 displacement = velocity * Time.deltaTime;
  13.         transform.localPosition += displacement;
  14.     [SerializeField, Range(1f, 1000f)]
  15.     float maxSpeed = 20f;
  16.     Vector3 velocity =
  17.             new Vector3(playerInput.x, 1f, playerInput.y) * maxSpeed;
  18.     Vector3 acceleration =
  19.             new Vector3(playerInput.x, 0f, playerInput.y) * maxSpeed;
  20.         velocity += acceleration * Time.deltaTime;
  21.         Vector3 displacement = velocity * Time.deltaTime;
  22.         [SerializeField, Range(1f, 1000f)]
  23.     float maxAcceleration = 20f;
  24.     Vector3 desiredVelocity =
  25.             new Vector3(playerInput.x, 0f, playerInput.y) * maxSpeed;
  26.         float maxSpeedChange = maxAcceleration * Time.deltaTime;
  27.     float maxSpeedChange = maxAcceleration * Time.deltaTime;
  28.     velocity.x =
  29.             Mathf.MoveTowards(velocity.x, desiredVelocity.x, maxSpeedChange);
  30.         velocity.z =
  31.             Mathf.MoveTowards(velocity.z, desiredVelocity.z, maxSpeedChange);
  32.     [SerializeField]
  33.     Rect allowedArea = new Rect(-5f, -5f, 10f, 10f);
  34.     Vector3 newPosition = transform.localPosition + displacement;
  35.         transform.localPosition = newPosition;
  36.     Vector3 newPosition = transform.localPosition + displacement;
  37.         if (newPosition.x < allowedArea.xMin) {
  38.             newPosition.x = allowedArea.xMin;
  39.             velocity.x = -velocity.x;
  40.         }
  41.         else if (newPosition.x > allowedArea.xMax) {
  42.             newPosition.x = allowedArea.xMax;
  43.             velocity.x = -velocity.x;
  44.         }
  45.         if (newPosition.z < allowedArea.yMin) {
  46.             newPosition.z = allowedArea.yMin;
  47.             velocity.z = -velocity.z;
  48.         }
  49.         else if (newPosition.z > allowedArea.yMax) {
  50.             newPosition.z = allowedArea.yMax;
  51.             velocity.z = -velocity.z;
  52.         }
  53.     if (newPosition.x < allowedArea.xMin) {
  54.             newPosition.x = allowedArea.xMin;
  55.             velocity.x = -velocity.x * bounciness;
  56.         }
  57.         else if (newPosition.x > allowedArea.xMax) {
  58.             newPosition.x = allowedArea.xMax;
  59.             velocity.x = -velocity.x * bounciness;
  60.         }
  61.         if (newPosition.z < allowedArea.yMin) {
  62.             newPosition.z = allowedArea.yMin;
  63.             velocity.z = -velocity.z * bounciness;
  64.         }
  65.         else if (newPosition.z > allowedArea.yMax) {
  66.             newPosition.z = allowedArea.yMax;
  67.             velocity.z = -velocity.z * bounciness;
  68.         }
  69.     //This is a very old script I made a year ago in Unity3D and never used so apologies if it doesn't work, I just learnt it from an old YouTube tutorial so it may be broken!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement