Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Yuterk · Apr 02, 2021 at 10:55 PM · raycastjumping

Why this don't work in unity 2020.3.1f1, but it´s working in 2019.2.14f1?

I follow a course for understand the physics in unity, but i really don't understand why I can't snap object to the ground automatically, and why this code affect how jump work. I already test the code in oldest version of unity and work pretty well so far, my question is, are alternative to create the same code for unity 2020.1f1?

Blockquote using UnityEngine;

public class MovingSphere : MonoBehaviour {

 [SerializeField, Range(0f, 100f)]
 float maxSpeed = 10f;

 [SerializeField, Range(0f, 100f)]
 float maxAcceleration = 10f, maxAirAcceleration = 1f;

 [SerializeField, Range(0f, 10f)]
 float jumpHeight = 2f;

 [SerializeField, Range(0, 5)]
 int maxAirJumps = 0;

 [SerializeField, Range(0, 90)]
 float maxGroundAngle = 25f, maxStairsAngle = 50f;

 [SerializeField, Range(0f, 100f)]
 float maxSnapSpeed = 100f;

 [SerializeField, Min(0f)]
 float probeDistance = 1f;

 [SerializeField]
 LayerMask probeMask = -1, stairsMask = -1;

 Rigidbody body;

 Vector3 velocity, desiredVelocity;

 bool desiredJump;

 Vector3 contactNormal, steepNormal;

 int groundContactCount, steepContactCount;

 bool OnGround => groundContactCount > 0;

 bool OnSteep => steepContactCount > 0;

 int jumpPhase;

 float minGroundDotProduct, minStairsDotProduct;

 int stepsSinceLastGrounded, stepsSinceLastJump;

 void OnValidate () {
     minGroundDotProduct = Mathf.Cos(maxGroundAngle * Mathf.Deg2Rad);
     minStairsDotProduct = Mathf.Cos(maxStairsAngle * Mathf.Deg2Rad);
 }

 void Awake () {
     body = GetComponent<Rigidbody>();
     OnValidate();
 }

 void Update () {
     Vector2 playerInput;
     playerInput.x = Input.GetAxis("Horizontal");
     playerInput.y = Input.GetAxis("Vertical");
     playerInput = Vector2.ClampMagnitude(playerInput, 1f);

     desiredVelocity =
         new Vector3(playerInput.x, 0f, playerInput.y) * maxSpeed;

     desiredJump |= Input.GetButtonDown("Jump");
 }

 void FixedUpdate () {
     UpdateState();
     AdjustVelocity();

     if (desiredJump) {
         desiredJump = false;
         Jump();
     }

     body.velocity = velocity;
     ClearState();
 }

 void ClearState () {
     groundContactCount = steepContactCount = 0;
     contactNormal = steepNormal = Vector3.zero;
 }

 void UpdateState () {
     stepsSinceLastGrounded += 1;
     stepsSinceLastJump += 1;
     velocity = body.velocity;
     if (OnGround || SnapToGround() || CheckSteepContacts()) {
         stepsSinceLastGrounded = 0;
         if (stepsSinceLastJump > 1) {
             jumpPhase = 0;
         }
         if (groundContactCount > 1) {
             contactNormal.Normalize();
         }
     }
     else {
         contactNormal = Vector3.up;
     }
 }

 bool SnapToGround () {
     if (stepsSinceLastGrounded > 1 || stepsSinceLastJump <= 2) {
         return false;
     }
     float speed = velocity.magnitude;
     if (speed > maxSnapSpeed) {
         return false;
     }
     if (!Physics.Raycast(
         body.position, Vector3.down, out RaycastHit hit,
         probeDistance, probeMask
     )) {
         return false;
     }
     if (hit.normal.y < GetMinDot(hit.collider.gameObject.layer)) {
         return false;
     }

     groundContactCount = 1;
     contactNormal = hit.normal;
     float dot = Vector3.Dot(velocity, hit.normal);
     if (dot > 0f) {
         velocity = (velocity - hit.normal * dot).normalized * speed;
     }
     return true;
 }

 bool CheckSteepContacts () {
     if (steepContactCount > 1) {
         steepNormal.Normalize();
         if (steepNormal.y >= minGroundDotProduct) {
             steepContactCount = 0;
             groundContactCount = 1;
             contactNormal = steepNormal;
             return true;
         }
     }
     return false;
 }

 void AdjustVelocity () {
     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 Jump () {
     Vector3 jumpDirection;
     if (OnGround) {
         jumpDirection = contactNormal;
     }
     else if (OnSteep) {
         jumpDirection = steepNormal;
         jumpPhase = 0;
     }
     else if (maxAirJumps > 0 && jumpPhase <= maxAirJumps) {
         if (jumpPhase == 0) {
             jumpPhase = 1;
         }
         jumpDirection = contactNormal;
     }
     else {
         return;
     }

     stepsSinceLastJump = 0;
     jumpPhase += 1;
     float jumpSpeed = Mathf.Sqrt(-2f * Physics.gravity.y * jumpHeight);
     jumpDirection = (jumpDirection + Vector3.up).normalized;
     float alignedSpeed = Vector3.Dot(velocity, jumpDirection);
     if (alignedSpeed > 0f) {
         jumpSpeed = Mathf.Max(jumpSpeed - alignedSpeed, 0f);
     }
     velocity += jumpDirection * jumpSpeed;
 }

 void OnCollisionEnter (Collision collision) {
     EvaluateCollision(collision);
 }

 void OnCollisionStay (Collision collision) {
     EvaluateCollision(collision);
 }

 void EvaluateCollision (Collision collision) {
     float minDot = GetMinDot(collision.gameObject.layer);
     for (int i = 0; i < collision.contactCount; i++) {
         Vector3 normal = collision.GetContact(i).normal;
         if (normal.y >= minDot) {
             groundContactCount += 1;
             contactNormal += normal;
         }
         else if (normal.y > -0.01f) {
             steepContactCount += 1;
             steepNormal += normal;
         }
     }
 }

 Vector3 ProjectOnContactPlane (Vector3 vector) {
     return vector - contactNormal * Vector3.Dot(vector, contactNormal);
 }

 float GetMinDot (int layer) {
     return (stairsMask & (1 << layer)) == 0 ?
         minGroundDotProduct : minStairsDotProduct;
 }

}

stairs.txt (5.5 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

205 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why isn't my raycast and debug.drawline working properly? 0 Answers

Simple Jumping Problem with raycast,Simple Jumping Problem 1 Answer

Raycast not changing value 0 Answers

Jump Using Raycast. 0 Answers

Problem with double jump and characther movement 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges