Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class gravity : MonoBehaviour {
- public float _gravity;
- public float mass;
- public bool noAirResistance = false;
- private Rigidbody rb;
- private float startingPos;
- private bool grounded = false;
- private float denisty = 1.29f;
- [SerializeField]private float border;
- [SerializeField]private float fallingVelocity;
- private void Start()
- {
- border = 2 * (mass * _gravity) / (0.4f * 1 * 3.14f * denisty);
- startingPos = this.transform.position.y;
- _gravity = (_gravity > 0) ? _gravity : _gravity * (-1);
- rb = this.gameObject.GetComponent<Rigidbody>();
- }
- private void Update()
- {
- if(noAirResistance)
- {
- if (fallingVelocity < 0.5) grounded = false;
- fallingVelocity = calculateFallingVelocity();
- if (!grounded)
- rb.velocity = new Vector3(0, (-1) * fallingVelocity, 0);
- else rb.velocity = new Vector3(0, 1 * fallingVelocity, 0);
- //Debug.Log(fallingVelocity);
- }
- else
- {
- if (!grounded)
- {
- fallingVelocity = calculateFallingVelocity();
- //Debug.Log(fallingVelocity);
- //if (fallingVelocity < border) fallingVelocity = border;
- rb.velocity = new Vector3(0, (-1) * fallingVelocity, 0);
- }
- else
- {
- fallingVelocity = calculateFallingVelocity();
- rb.velocity = new Vector3(0, (1) * fallingVelocity, 0);
- if (fallingVelocity < 1)
- {
- grounded = false;
- startingPos = this.transform.position.y;
- }
- }
- }
- }
- private float calculateFallingVelocity()
- {
- float velocity = startingPos + (_gravity * (calculateTime())) / 2;
- return velocity;
- }
- private float calculateTime()
- {
- float time = (2 * (startingPos - this.transform.position.y)) / _gravity;
- Debug.Log(time);
- return time;
- }
- void OnCollisionEnter(Collision c)
- {
- grounded = true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement