Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class ramp : MonoBehaviour
  6. {
  7. // Start is called before the first frame update
  8. GameObject Weight;
  9. GameObject Ramp;
  10. Vector2 Velocity;
  11. float m = 20;
  12. float u = 0.15f;
  13. public float Fn;
  14. public float Fu;
  15. public float Fx;
  16. public float Fr;
  17.  
  18. public float Fg;
  19. float a;
  20. float cosa;
  21.  
  22. void Start()
  23. {
  24. Velocity = new Vector2(0, 0);
  25.  
  26. Weight = GameObject.Find("Weight");
  27. Ramp = GameObject.Find("Ramp");
  28. }
  29.  
  30. // Update is called once per frame
  31. void Update()
  32. {
  33. float angle = 360 - transform.localEulerAngles.z;
  34.  
  35.  
  36. Fg = m * 9.82f;
  37. cosa = Mathf.Cos(angle*Mathf.Deg2Rad);
  38. Fn = cosa * Fg; //Normalkraften Fn
  39.  
  40. //Fu = -(u * Fn); //Friktionskraften Fu
  41. Fu = m * 9.82f * u;
  42.  
  43. Fx = Mathf.Sin(angle* Mathf.Deg2Rad) * Fg; //Kraften som drar den framåt Fx
  44. //float Fx1 = Mathf.Abs(Fx);
  45.  
  46. Fr = Fx - Fu; //Den resulterande kraften
  47.  
  48.  
  49. a = Fr / m; //resulterande accelerationen
  50.  
  51.  
  52.  
  53.  
  54. if (Input.GetKeyDown(KeyCode.UpArrow))
  55. {
  56. transform.Rotate(0, 0, -2);
  57. }
  58.  
  59. if (Input.GetKeyDown(KeyCode.DownArrow))
  60. {
  61. transform.Rotate(0, 0, +2);
  62. }
  63.  
  64.  
  65.  
  66.  
  67.  
  68. //Accelerationen ska beräknas enligt de krafter som påverkar vikten (tyngdkraft, normalkraft, friktionskraft)
  69. //O,1 unity enheter per sekund (i kvadrat)
  70. float ar = 0f;
  71.  
  72. if (Fx > Fu || Velocity.x > 0)
  73. {
  74. Fr = Fx - Fu;
  75. ar = Fr / m;
  76. ar = ar * Time.deltaTime;
  77.  
  78. }
  79.  
  80.  
  81. Weight.transform.Translate(Velocity);
  82.  
  83. Velocity.x += ar;
  84.  
  85.  
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement