Advertisement
KodingKid

C# Unity3D Ball Physics - Advanced

Apr 5th, 2021
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.17 KB | None | 0 0
  1. Vector3 newPosition = transform.localPosition + displacement;
  2. transform.localPosition = newPosition;
  3. velocity = body.velocity;
  4.         float maxSpeedChange = maxAcceleration * Time.deltaTime;
  5.         velocity.x =
  6.             Mathf.MoveTowards(velocity.x, desiredVelocity.x, maxSpeedChange);
  7.         velocity.z =
  8.             Mathf.MoveTowards(velocity.z, desiredVelocity.z, maxSpeedChange);
  9.         body.velocity = velocity;
  10. void Update () {
  11.         Vector2 playerInput;
  12.         playerInput.x = Input.GetAxis("Horizontal");
  13.         playerInput.y = Input.GetAxis("Vertical");
  14.         playerInput = Vector2.ClampMagnitude(playerInput, 2f);
  15. new Vector3(playerInput.x, 1f, playerInput.y) * maxSpeed;
  16.     }
  17. void FixedUpdate () {
  18.         velocity = body.velocity;
  19.         float maxSpeedChange = maxAcceleration * Time.deltaTime;
  20.         velocity.x =
  21.             Mathf.MoveTowards(velocity.x, desiredVelocity.x, maxSpeedChange);
  22.         velocity.z =
  23.             Mathf.MoveTowards(velocity.z, desiredVelocity.z, maxSpeedChange);
  24.         body.velocity = velocity;
  25.     }
  26. bool desiredJump;
  27. void Update () {
  28.     desiredJump |= Input.GetButtonDown("Jump");
  29.     void FixedUpdate () {
  30.         if (desiredJump) {
  31.             desiredJump = false;
  32.             Jump();
  33.         }
  34.         body.velocity = velocity;
  35.     }
  36.    
  37.     void Jump() {
  38.         velocity.y += 5f;
  39.     }  
  40.         [SerializeField, Range(1f, 100f)]
  41.     float jumpHeight = 3f;
  42.     void Jump () {
  43.         velocity.y += Mathf.Sqrt(-3f * Physics.gravity.y * jumpHeight);
  44.     }
  45.     bool onGround;
  46.         void OnCollisionEnter (Collision collision) {
  47.         EvaluateCollision(collision);
  48.     }
  49.     void OnCollisionStay () {
  50.         onGround = true;
  51.     }
  52.     void Jump () {
  53.         if (onGround) {
  54.             velocity.y += Mathf.Sqrt(-3f * Physics.gravity.y * jumpHeight);
  55.         }
  56.     }
  57.     void FixedUpdate () {
  58.         onGround = false;
  59.     }
  60.     Vector3 normal = collision.GetContact(i).normal;
  61.             onGround |= normal.y >= 0.9f;
  62.     [SerializeField, Range(0, 5)]
  63.     int maxAirJumps = 0;
  64.     int jumpPhase;
  65.     void FixedUpdate () {
  66.         UpdateState();
  67.     }
  68.     void UpdateState () {
  69.         velocity = body.velocity;
  70.         if (onGround) {
  71.             jumpPhase = 0;
  72.         }
  73.     }
  74.     void Jump () {
  75.         if (onGround || jumpPhase < maxAirJumps) {
  76.             jumpPhase += 1;
  77.             velocity.y += Mathf.Sqrt(-3f * Physics.gravity.y * jumpHeight);
  78.         }
  79.     }
  80.     jumpPhase += 1;
  81.     float jumpSpeed = Mathf.Sqrt(-3f * Physics.gravity.y * jumpHeight);
  82.             if (velocity.y > 0f) {
  83.                 jumpSpeed = Mathf.Max(jumpSpeed - velocity.y, 0f);
  84.             }
  85.             velocity.y += jumpSpeed;
  86.     [SerializeField, Range(1f, 100f)]
  87.     float maxAcceleration = 20f, float acceleration = onGround ? maxAcceleration : maxAirAcceleration;
  88.         float maxSpeedChange = acceleration * Time.deltaTime;
  89.     [SerializeField, Range(0f, 90f)]
  90.     float maxGroundAngle = 25f;
  91.     float minGroundDotProduct;
  92.     void OnValidate () {
  93.         minGroundDotProduct = Mathf.Cos(maxGroundAngle);
  94.     }
  95.     void Awake () {
  96.         body = GetComponent<Rigidbody>();
  97.         OnValidate();
  98.     }
  99.     minGroundDotProduct = Mathf.Cos(maxGroundAngle * Mathf.Deg2Rad);
  100.     void EvaluateCollision (Collision collision) {
  101.         for (int i = 0; i < collision.contactCount; i++) {
  102.             Vector3 normal = collision.GetContact(i).normal;
  103.             if (normal.y >= minGroundDotProduct) {
  104.                 groundContactCount += 1;
  105.                 contactNormal += normal;
  106.             }
  107.         }
  108.     }
  109.    void UpdateState () {
  110.         velocity = body.velocity;
  111.         if (onGround) {
  112.             jumpPhase = 0;
  113.         }
  114.         else {
  115.             contactNormal = Vector3.up;
  116.         }
  117.     }
  118.     void UpdateState () {
  119.         velocity = body.velocity;
  120.         if (onGround) {
  121.             jumpPhase = 0;
  122.         }
  123.         else {
  124.             contactNormal = Vector3.up;
  125.         }
  126.     }
  127.     void Jump () {
  128.         if (onGround || jumpPhase < maxAirJumps) {
  129.             velocity += contactNormal * jumpSpeed;
  130.         }
  131.     }
  132.     float jumpSpeed = Mathf.Sqrt(-3f * Physics.gravity.y * jumpHeight);
  133.             float alignedSpeed = Vector3.Dot(velocity, contactNormal);
  134.             if (alignedSpeed > 0f) {
  135.                 jumpSpeed = Mathf.Max(jumpSpeed - alignedSpeed, 0f);
  136.             }
  137.             velocity += contactNormal * jumpSpeed;
  138.     Vector3 ProjectOnContactPlane (Vector3 vector) {
  139.         return vector - contactNormal * Vector3.Dot(vector, contactNormal);
  140.     }
  141.     void AdjustVelocity () {
  142.         Vector3 xAxis = ProjectOnContactPlane(Vector3.right);
  143.         Vector3 zAxis = ProjectOnContactPlane(Vector3.forward);
  144.     }
  145.     Vector3 xAxis = ProjectOnContactPlane(Vector3.right).normalized;
  146.     Vector3 zAxis = ProjectOnContactPlane(Vector3.forward).normalized;
  147.         float currentX = Vector3.Dot(velocity, xAxis);
  148.         float currentZ = Vector3.Dot(velocity, zAxis);
  149.     float acceleration = onGround ? maxAcceleration : maxAirAcceleration;
  150.         float maxSpeedChange = acceleration * Time.deltaTime;
  151.         float newX =
  152.             Mathf.MoveTowards(currentX, desiredVelocity.x, maxSpeedChange);
  153.         float newZ =
  154.             Mathf.MoveTowards(currentZ, desiredVelocity.z, maxSpeedChange);
  155.     velocity += xAxis * (newX - currentX) + zAxis * (newZ - currentZ);
  156.     void ClearState () {
  157.         groundContactCount = 0;
  158.         contactNormal = Vector3.zero;
  159.     }
  160.     void UpdateState () {
  161.         velocity = body.velocity;
  162.         if (OnGround) {
  163.             jumpPhase = 0;
  164.             if (groundContactCount > 1) {
  165.                 contactNormal.Normalize();
  166.             }
  167.         }
  168.     }
  169. //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! (same as: https://pastebin.com/Sj7i5JiU)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement