Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Vector3 newPosition = transform.localPosition + displacement;
- transform.localPosition = newPosition;
- velocity = body.velocity;
- float maxSpeedChange = maxAcceleration * Time.deltaTime;
- velocity.x =
- Mathf.MoveTowards(velocity.x, desiredVelocity.x, maxSpeedChange);
- velocity.z =
- Mathf.MoveTowards(velocity.z, desiredVelocity.z, maxSpeedChange);
- body.velocity = velocity;
- void Update () {
- Vector2 playerInput;
- playerInput.x = Input.GetAxis("Horizontal");
- playerInput.y = Input.GetAxis("Vertical");
- playerInput = Vector2.ClampMagnitude(playerInput, 2f);
- new Vector3(playerInput.x, 1f, playerInput.y) * maxSpeed;
- }
- void FixedUpdate () {
- velocity = body.velocity;
- float maxSpeedChange = maxAcceleration * Time.deltaTime;
- velocity.x =
- Mathf.MoveTowards(velocity.x, desiredVelocity.x, maxSpeedChange);
- velocity.z =
- Mathf.MoveTowards(velocity.z, desiredVelocity.z, maxSpeedChange);
- body.velocity = velocity;
- }
- bool desiredJump;
- void Update () {
- desiredJump |= Input.GetButtonDown("Jump");
- void FixedUpdate () {
- if (desiredJump) {
- desiredJump = false;
- Jump();
- }
- body.velocity = velocity;
- }
- void Jump() {
- velocity.y += 5f;
- }
- [SerializeField, Range(1f, 100f)]
- float jumpHeight = 3f;
- void Jump () {
- velocity.y += Mathf.Sqrt(-3f * Physics.gravity.y * jumpHeight);
- }
- bool onGround;
- void OnCollisionEnter (Collision collision) {
- EvaluateCollision(collision);
- }
- void OnCollisionStay () {
- onGround = true;
- }
- void Jump () {
- if (onGround) {
- velocity.y += Mathf.Sqrt(-3f * Physics.gravity.y * jumpHeight);
- }
- }
- void FixedUpdate () {
- onGround = false;
- }
- Vector3 normal = collision.GetContact(i).normal;
- onGround |= normal.y >= 0.9f;
- [SerializeField, Range(0, 5)]
- int maxAirJumps = 0;
- int jumpPhase;
- void FixedUpdate () {
- UpdateState();
- }
- void UpdateState () {
- velocity = body.velocity;
- if (onGround) {
- jumpPhase = 0;
- }
- }
- void Jump () {
- if (onGround || jumpPhase < maxAirJumps) {
- jumpPhase += 1;
- velocity.y += Mathf.Sqrt(-3f * Physics.gravity.y * jumpHeight);
- }
- }
- jumpPhase += 1;
- float jumpSpeed = Mathf.Sqrt(-3f * Physics.gravity.y * jumpHeight);
- if (velocity.y > 0f) {
- jumpSpeed = Mathf.Max(jumpSpeed - velocity.y, 0f);
- }
- velocity.y += jumpSpeed;
- [SerializeField, Range(1f, 100f)]
- float maxAcceleration = 20f, float acceleration = onGround ? maxAcceleration : maxAirAcceleration;
- float maxSpeedChange = acceleration * Time.deltaTime;
- [SerializeField, Range(0f, 90f)]
- float maxGroundAngle = 25f;
- float minGroundDotProduct;
- void OnValidate () {
- minGroundDotProduct = Mathf.Cos(maxGroundAngle);
- }
- void Awake () {
- body = GetComponent<Rigidbody>();
- OnValidate();
- }
- minGroundDotProduct = Mathf.Cos(maxGroundAngle * Mathf.Deg2Rad);
- void EvaluateCollision (Collision collision) {
- for (int i = 0; i < collision.contactCount; i++) {
- Vector3 normal = collision.GetContact(i).normal;
- if (normal.y >= minGroundDotProduct) {
- groundContactCount += 1;
- contactNormal += normal;
- }
- }
- }
- void UpdateState () {
- velocity = body.velocity;
- if (onGround) {
- jumpPhase = 0;
- }
- else {
- contactNormal = Vector3.up;
- }
- }
- void UpdateState () {
- velocity = body.velocity;
- if (onGround) {
- jumpPhase = 0;
- }
- else {
- contactNormal = Vector3.up;
- }
- }
- void Jump () {
- if (onGround || jumpPhase < maxAirJumps) {
- velocity += contactNormal * jumpSpeed;
- }
- }
- float jumpSpeed = Mathf.Sqrt(-3f * Physics.gravity.y * jumpHeight);
- float alignedSpeed = Vector3.Dot(velocity, contactNormal);
- if (alignedSpeed > 0f) {
- jumpSpeed = Mathf.Max(jumpSpeed - alignedSpeed, 0f);
- }
- velocity += contactNormal * jumpSpeed;
- Vector3 ProjectOnContactPlane (Vector3 vector) {
- return vector - contactNormal * Vector3.Dot(vector, contactNormal);
- }
- void AdjustVelocity () {
- Vector3 xAxis = ProjectOnContactPlane(Vector3.right);
- Vector3 zAxis = ProjectOnContactPlane(Vector3.forward);
- }
- Vector3 xAxis = ProjectOnContactPlane(Vector3.right).normalized;
- Vector3 zAxis = ProjectOnContactPlane(Vector3.forward).normalized;
- float currentX = Vector3.Dot(velocity, xAxis);
- float currentZ = Vector3.Dot(velocity, zAxis);
- float acceleration = onGround ? maxAcceleration : maxAirAcceleration;
- float maxSpeedChange = acceleration * Time.deltaTime;
- float newX =
- Mathf.MoveTowards(currentX, desiredVelocity.x, maxSpeedChange);
- float newZ =
- Mathf.MoveTowards(currentZ, desiredVelocity.z, maxSpeedChange);
- velocity += xAxis * (newX - currentX) + zAxis * (newZ - currentZ);
- void ClearState () {
- groundContactCount = 0;
- contactNormal = Vector3.zero;
- }
- void UpdateState () {
- velocity = body.velocity;
- if (OnGround) {
- jumpPhase = 0;
- if (groundContactCount > 1) {
- contactNormal.Normalize();
- }
- }
- }
- //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