• 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 /
avatar image
0
Question by Unity Gamer · Sep 06, 2013 at 06:20 AM · physicsslope

Movement up and down a slope, and

Hey, am trying to move an object along a series of slopes, and also make it follow a trajectory when it reaches a certain velocity at the apex of a slope. Am having a few issues with my code. I would really appreciate it someone could just check and see if there is anything wrong with it.

When I accelerate up a slope, it accelerates but on reaching the top or the apex of the slope it starts to move back down. And when I accelerate down a slope it moves very slowly or doesn't move at all (it should be moving at a greater acceleration).

What is the appropriate condition or process to use to synch all the movement together (such that it is smooth and seamless).

     public void Accelerate(Vector3 slopeNormal)
     {
         if(MoveDirection.magnitude > 0f)
         {
             Vector3 targetVelocity ;
             Vector3 currentVelocity ;
             Vector3 velocityChange ;
             Vector3 force ;
             Vector3 a ;
             
             float levelStopTime ;
             float brakingDistance ;
                         
             targetVelocity = Speed * MoveDirection ;
             currentVelocity = Character.rigidbody.velocity;    
             
             velocityChange = targetVelocity - currentVelocity;    
             
             a.x = Mathf.Clamp(velocityChange.x, -MaxAcceleration, MaxAcceleration);
             a.y = Mathf.Clamp(velocityChange.y, -MaxAcceleration, MaxAcceleration);
             a.z = Mathf.Clamp(velocityChange.z, -MaxAcceleration, MaxAcceleration);
             Acceleration = a.magnitude * Mathf.Pow (Time.deltaTime, 2f);    // Not sure, CHECK!!
             
             force = Character.rigidbody.mass * a ;
 
 //            levelStopTime = (m_character.rigidbody.velocity / m_maxAcceleration).magnitude ;
 //            brakingDistance = (m_character.rigidbody.velocity.magnitude * m_levelStopTime) + (m_maxAcceleration * m_levelStopTime) ;//Mathf.Pow(m_levelStopTime, 2f) / 2f) ;                    
             
             if(Input.GetKey(KeyCode.U))
                 AccelerateUpSlope(slopeNormal) ;
             else if(Input.GetKey(KeyCode.D))
                 AccelerateDownSlope(slopeNormal) ;
             else
                 Character.rigidbody.AddForce(force) ;
         }
         else
             Debug.LogError("Movement Direction Invalid!!") ;
     }
     
     public void AccelerateDownSlope(Vector3 slopeNormal)
     {
         float fG, fNet, fParallel ;
         float m ;
         float g ;
         float theta ;
         
         float a ;
         Vector3 A ;
 
         Vector3 characterNormal = Character.rigidbody.velocity.normalized ;
         
         m = Character.rigidbody.mass ;
         g = Physics.gravity.y ;
 
         fG = m * g;
         theta = Vector3.Angle(characterNormal, slopeNormal) ;
         
         fParallel = fG * Mathf.Sin(theta) ;
         fNet = fParallel ;
         
         a = fNet / m ;
         A = a * new Vector3(1f, 1f, 0f) ;
         Debug.Log("Down " + A) ;
         
         Acceleration = a  * Mathf.Pow (Time.deltaTime, 2f);    // Not sure, CHECK!!
         Character.rigidbody.AddForce(A, ForceMode.Acceleration) ;
     }
     
     public void AccelerateUpSlope(Vector3 slopeNormal)
     {
         float fG, fNet, fParallel, fPerpendicular ;
         float m ;
         float g ;
         float theta ;
         
         float a ;
         Vector3 A ;
 
         Vector3 characterNormal = Character.rigidbody.velocity.normalized ;
         
         m = Character.rigidbody.mass ;
         g = Physics.gravity.y ;
 
         fG = m * g;
         theta = Vector3.Angle(characterNormal, slopeNormal) ;
         
         Debug.Log(theta) ;        
         
         fParallel = fG * Mathf.Sin(theta) ;
         fPerpendicular = CoefficentFriction * fG * Mathf.Cos(theta) ; 
         
         fNet = fParallel - fPerpendicular ;
         
         a = fNet / m ;
         A = a * new Vector3(1f, 1f, 0f) ;
         Debug.Log("UP " + A) ;        
         
         Acceleration = a  * Mathf.Pow (Time.deltaTime, 2f);    // Not sure, CHECK!!
         Character.rigidbody.AddForce(A, ForceMode.Acceleration) ;
     }    
         
     public void ProjectileMotion(Vector3 colliderNormal)
     {
         float maxVelocityX = 45f ;
         float maxVelocityY = 60f ;
         Vector3 initialVelocity = go.rigidbody.velocity ;
         Vector3 initialPosition = go.rigidbody.position ;
         
 //        float angle = Vector3.Angle(go.rigidbody.position.normalized, colliderNormal) ;
 //        float launchAngleRad = (angle * Mathf.PI) / 180f ;
 
         float launchAngleRad = (m_launchAngle * Mathf.PI) / 180f ;
         float timeOfFlight = (2f * initialVelocity.magnitude * Mathf.Sin(launchAngleRad)) / Physics.gravity.magnitude ;
         
 //        Debug.Log("Launch Angle (rad) - " + launchAngleRad) ;
 //        Debug.Log("Time of Flight - " + timeOfFlight) ;
         
         float x, y ;        
         
         x = initialPosition.x + (initialVelocity.x * Mathf.Cos(launchAngleRad) * timeOfFlight) ;
         y = initialPosition.y + (initialVelocity.y * Mathf.Sin(launchAngleRad) * timeOfFlight) - (0.5f * Physics.gravity.y * Mathf.Pow(timeOfFlight, 2)) ;
         
 //        Debug.Log("x Position - " + x) ;
 //        Debug.Log("y Position - " + y) ;            
         
         Vector3 amtToMove = new Vector3(x, y, go.transform.position.z) ;
         Vector3 finalVelocity = amtToMove / timeOfFlight ;
 
 //        go.rigidbody.MovePosition(amtToMove) ;
         
         if(finalVelocity.x >= 0f)
         {
 //            Debug.Log(finalVelocity) ;
             
             finalVelocity.x = Mathf.Clamp(finalVelocity.x, -maxVelocityX, maxVelocityX);
             finalVelocity.y = Mathf.Clamp(finalVelocity.y, -maxVelocityY, maxVelocityY);            
             
             go.rigidbody.AddForce(finalVelocity, ForceMode.VelocityChange) ;    // ForceMode.Acceleration) ;
         }
     }

Also would this be the best way to check if the object is moving on a slope.

     public void CollisionCheck(GameObject go)
     {
         Ray ray ;
         RaycastHit hit ;
         float x, y ;
 //        float offset = 10f ;
         float playerGroundSpace = 0.0005f ;    
         
         CapsuleCollider collider = go.GetComponent<CapsuleCollider>() ;
         
         // Checking for collision up and down                                                                
         x = go.transform.position.x - go.transform.localScale.x / 2f ; 
         y = go.transform.position.y + go.transform.localScale.y / 2f ;
         
         ray = new Ray(new Vector2(x, y), new Vector2(0f, 10f)) ;
         
         Debug.DrawRay(ray.origin, ray.direction);
             
         if (Physics.Raycast(ray, out hit))
         {
             float dst = Vector3.Distance(ray.origin, hit.point) ;
 
             if(dst > playerGroundSpace)
             {
                 Debug.Log("Collision, UP/DOWN") ;
             }
         }
         
         // Checking for collision left and right        
         x = go.transform.position.x + go.transform.localScale.x / 2f ;
         y = go.transform.position.y - go.transform.localScale.y / 2f ;
         
         ray = new Ray(new Vector2(x, y), new Vector2(10, 0)) ;
         
         Debug.DrawRay(ray.origin, ray.direction);
         
         if (Physics.Raycast(ray, out hit))
         {
             float dst = Vector3.Distance(ray.origin, hit.point) ;
         
             if(dst > playerGroundSpace)
             {
                 Debug.Log("Collision, LEFT/RIGHT") ;
             }        
         }
     }


Thanks for any help. Cheers!!

Comment
Add comment · Show 3
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
avatar image Sajidfarooq · Sep 06, 2013 at 06:27 AM 0
Share

Your question is very vague, and then followed by a wall of code, and then another.

What exactly does this mean? : make it follow a trajectory when it reaches a certain velocity at the apex of a slope.

Perhaps you could post a diagram/video/reference so we could see what you mean.

avatar image Unity Gamer · Sep 06, 2013 at 06:48 AM 0
Share

I want to move the object along the given slope, and I want it to accelerate up and down the slopes when required. When the slopes are small and close to neglegible, it will at a normal speed defined in the Accelerate(). And when the object goes off a slope I want at relatively higher speed I want it to follow a trajectory part, deter$$anonymous$$ed by its launch angle and current velocity.alt text

screen shot 2013-09-06 at 12.14.22 pm.png (46.1 kB)
avatar image Sajidfarooq · Sep 06, 2013 at 10:19 AM 0
Share

Isn't that what a physics object would do anyway if you simply applied a constant force to it as it bounced along a slope? It would slow down when going up, speed up when doing down, and fall through a trajectory if the slope changed suddenly.

What am I missing?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

A node in a childnode? 1 Answer

Physics-based controls and slopes ? 2 Answers

Unity Script causing crash 1 Answer

Rigidbody2D won't stop moving. 2 Answers

Addforce - bug with jump 2 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