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 /
avatar image
2
Question by Entyro · Apr 21, 2013 at 10:28 AM · rigidbodyplayerfootstepssliding

Rigidbody problem

Hi!

I have a problem when I'm using a rigidbody on my player. When I'm trying to walk up stairs (just a straight collider)I'm sliding down when I'm trying to get up. I know I can use CharacterController but I need the rigidbody because of a script I use, (different walking sound on different surfaces. And the footstep script are using another script called "Rigid Controller".

Does anyone know what the problem is?

Rigid controller:

 /**
 *   [www.armedunity.com]
 **/
 
 var crouchSpeed = 2.0;
 var walkSpeed = 8.0;
 var runSpeed = 20.0;
 
 var fallDamageMultiplier : int = 2;
 //var fallAnimGO : GameObject;
 var inAirControl = 0.1;
 var gravity = 20.0;
 var maxVelocityChange = 10.0;
 var canJump = true;
 var jumpHeight = 2.0;
 var fallSound : AudioClip;
 var playerWeapons : GameObject;
 //@HideInInspector
 var grounded = false;
 private var sliding : boolean = false;
 private var speed = 10.0;
 private var limitDiagonalSpeed = true;
 private var crouching : boolean;
 private var normalHeight : float = 0.5;
 private var crouchHeight : float = -0.2;
 private var crouchingHeight = 0.3;
 private var hit : RaycastHit;
 private var myTransform : Transform;
 private var rayDistance : float;
 private var mainCameraGO : GameObject;
 private var weaponCameraGO : GameObject;
 
 @script RequireComponent(Rigidbody)
 
 function Awake (){
     rigidbody.freezeRotation = true;
     rigidbody.useGravity = false;
     myTransform = transform;
     mainCameraGO = gameObject.FindWithTag("MainCamera");
     rayDistance = 1.1;//collider.radius;
 
 }
 
 function FixedUpdate (){
     if (grounded){            
         var inputX = Input.GetAxis("Horizontal");
         var inputY = Input.GetAxis("Vertical");
         var inputModifyFactor = (inputX != 0.0 && inputY != 0.0 && limitDiagonalSpeed)? .7071 : 1.0;
         
         if (Physics.Raycast(myTransform.position, -Vector3.up, hit, rayDistance)) {
             if (Vector3.Angle(hit.normal, Vector3.up) > 30){
                 sliding = true;
                 rigidbody.AddRelativeForce (-Vector3.up * 500);
             }else{
                 sliding = false;
                 
             }    
         }
 
         // Calculate how fast we should be moving
         var targetVelocity = new Vector3(inputX * inputModifyFactor, 0.0, inputY * inputModifyFactor);
         targetVelocity = myTransform.TransformDirection(targetVelocity);
         targetVelocity *= speed;    
         
         // Apply a force that attempts to reach our target velocity
         var velocity = rigidbody.velocity;
         var velocityChange = (targetVelocity - velocity);
         velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
         velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
         velocityChange.y = 0;
         rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
    
         
         if (canJump && Input.GetButton("Jump")){
             rigidbody.velocity = Vector3(velocity.x, CalculateJumpVerticalSpeed(), velocity.z);
         }
         
         if(!crouching){
             if (grounded && Input.GetButton("Run") && Input.GetKey("w"))
                 speed = runSpeed;
                 else 
                 speed = walkSpeed;
             }else{
             speed = crouchSpeed;
         }
     
     }else{
     
         // AirControl 
         targetVelocity = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
         targetVelocity = transform.TransformDirection(targetVelocity) * inAirControl;
         rigidbody.AddForce(targetVelocity, ForceMode.VelocityChange);
     } 
 
     // Gravity 
     rigidbody.AddForce(Vector3 (0, -gravity * rigidbody.mass, 0));
     grounded = false;
 }
 
 function OnCollisionStay (col : Collision){
         
     for(var contact : ContactPoint in col.contacts){
         if(Vector3.Angle(contact.normal, Vector3.up) < 45){
             grounded = true;
         }       
     }
 }
 function HitJumpPad(velocity: float) {
        rigidbody.velocity.z += velocity;
     }
 
 function OnCollisionEnter (collision : Collision){
     if(!grounded){
         var currSpeed : float = collision.relativeVelocity.magnitude;
         
         if (currSpeed > 20) {
             var damage : float = currSpeed * fallDamageMultiplier;
             Debug.Log ("FallDamage" + damage);
             SendMessage ("PlayerDamage", damage, SendMessageOptions.DontRequireReceiver);
         }    
     }    
 }
 
 function CalculateJumpVerticalSpeed (){
     return Mathf.Sqrt(2 * jumpHeight * gravity);
 }
 
 function Update(){
     
     if(mainCameraGO.transform.localPosition.y > normalHeight){
         mainCameraGO.transform.localPosition.y = normalHeight;
     } else if(mainCameraGO.transform.localPosition.y < crouchHeight){
         mainCameraGO.transform.localPosition.y = crouchHeight;
     }
 
     if (Input.GetButtonDown("Crouch") && !crouching) {
         collider.height = 1.5;
         collider.center = Vector3 (0, -0.25, 0);
         crouching = true;
     } 
 
     if(Input.GetButtonUp("Crouch") && crouching){
         collider.height = 2.0;
         collider.center = Vector3 (0, 0, 0);
         crouching = false;
     }
     
     if(crouching){
         if(mainCameraGO.transform.localPosition.y > crouchHeight){
             if(mainCameraGO.transform.localPosition.y - (crouchingHeight * Time.deltaTime/.1) < crouchHeight){
                 mainCameraGO.transform.localPosition.y = crouchHeight;
             } else {
                 mainCameraGO.transform.localPosition.y -= crouchingHeight * Time.deltaTime/.1;
             }
         }
 
     } else {
         if(mainCameraGO.transform.localPosition.y < normalHeight){
             if(mainCameraGO.transform.localPosition.y + (crouchingHeight * Time.deltaTime/.1) > normalHeight){
                 mainCameraGO.transform.localPosition.y = normalHeight;
             } else {
                 mainCameraGO.transform.localPosition.y += crouchingHeight * Time.deltaTime/.1;
             }
         }
     }
 }
 
 function Accelerate (accelerateY : float, accelerateZ : float){
     grounded = false;
     rigidbody.AddRelativeForce (0, accelerateY, accelerateZ);    
 }


Footsteps: /** Script made by OMA [www.armedunity.com] */

 var concrete : AudioClip[];
 var wood : AudioClip[];
 var dirt : AudioClip[];
 var metal : AudioClip[];
 private var step : boolean = true;
 var audioStepLengthWalk : float = 0.45;
 var audioStepLengthRun : float = 0.25;
 var walkSpeed : float = 8.0;
 var runSpeed : float = 12.0;
 
 function OnCollisionStay (col : Collision) {
 var controller : CharacterMotor = GetComponent(CharacterMotor);
 
     if (controller.grounded && rigidbody.velocity.magnitude > (walkSpeed-2) && rigidbody.velocity.magnitude < (walkSpeed+2) && col.gameObject.tag == "Concrete" && step == true || controller.grounded && rigidbody.velocity.magnitude < (walkSpeed+2) && rigidbody.velocity.magnitude > (walkSpeed-2) && col.gameObject.tag == "Untagged" && step == true ) {
         WalkOnConcrete();
     }else if (controller.grounded && rigidbody.velocity.magnitude > (runSpeed-2) && col.gameObject.tag == "Concrete" && step == true || controller.grounded && rigidbody.velocity.magnitude > (runSpeed-2) && col.gameObject.tag == "Untagged" && step == true ) {
         RunOnConcrete();
     }else if (controller.grounded && rigidbody.velocity.magnitude > (walkSpeed-2) && rigidbody.velocity.magnitude < (walkSpeed+2) && col.gameObject.tag == "Wood" && step == true) {
         WalkOnWood();
     }else if (controller.grounded && rigidbody.velocity.magnitude > (runSpeed-2) && col.gameObject.tag == "Wood" && step == true){
         RunOnWood();
     }else if (controller.grounded && rigidbody.velocity.magnitude > (walkSpeed-2) && rigidbody.velocity.magnitude < (walkSpeed+2) && col.gameObject.tag == "Dirt" && step == true) {
         WalkOnDirt();
     }else if (controller.grounded && rigidbody.velocity.magnitude > (runSpeed-2) && col.gameObject.tag == "Dirt" && step == true){
         RunOnDirt();
     }else if (controller.grounded && rigidbody.velocity.magnitude > (walkSpeed-2) && rigidbody.velocity.magnitude < (walkSpeed+2) && col.gameObject.tag == "Metal" && step == true) {
         WalkOnMetal();
     }else if (controller.grounded && rigidbody.velocity.magnitude > (runSpeed-2) && col.gameObject.tag == "Metal" && step == true){
         RunOnMetal();
     }
 }    
 
 
 /////////////////////////////////// CONCRETE ////////////////////////////////////////
 function WalkOnConcrete() {
     step = false;
     audio.clip = concrete[Random.Range(0, concrete.length)];
     audio.volume = .1;
     audio.Play();
     yield WaitForSeconds (audioStepLengthWalk);
     step = true;
 }
 
 function RunOnConcrete() {
     step = false;
     audio.clip = concrete[Random.Range(0, concrete.length)];
     audio.volume = .3;
     audio.Play();
     yield WaitForSeconds (audioStepLengthRun);
     step = true;
 }    
 
 
 ////////////////////////////////// WOOD /////////////////////////////////////////////
 function WalkOnWood() {
     step = false;
     audio.clip = wood[Random.Range(0, wood.length)];
     audio.volume = .1;
     audio.Play();
     yield WaitForSeconds (audioStepLengthWalk);
     step = true;
 }
 
 function RunOnWood() {
     step = false;
     audio.clip = wood[Random.Range(0, wood.length)];
     audio.volume = .3;
     audio.Play();
     yield WaitForSeconds (audioStepLengthRun);
     step = true;
 }
 
 
 /////////////////////////////////// DIRT //////////////////////////////////////////////
 function WalkOnDirt() {
     step = false;
     audio.clip = dirt[Random.Range(0, dirt.length)];
     audio.volume = .1;
     audio.Play();
     yield WaitForSeconds (audioStepLengthWalk);
     step = true;
 }
 
 function RunOnDirt() {
     step = false;
     audio.clip = dirt[Random.Range(0, dirt.length)];
     audio.volume = .1;
     audio.Play();
     yield WaitForSeconds (audioStepLengthRun);
     step = true;
 }
 
 
 ////////////////////////////////// METAL ///////////////////////////////////////////////
 function WalkOnMetal() {    
     step = false;
     audio.clip = metal[Random.Range(0, metal.length)];
     audio.volume = .1;
     audio.Play();
     yield WaitForSeconds (audioStepLengthWalk);
     step = true;
 }
 
 function RunOnMetal() {
     step = false;
     audio.clip = metal[Random.Range(0, metal.length)];
     audio.volume = .3;
     audio.Play();
     yield WaitForSeconds (audioStepLengthRun);
     step = true;
 }
 
 @script RequireComponent(AudioSource)



Sorry for long post...

Comment
Add comment · Show 10
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 whydoidoit · Apr 21, 2013 at 10:31 AM 1
Share

Use a CharacterController - add a Rigidbody to it and make the rigidbody is$$anonymous$$inematic = true

avatar image whydoidoit · Apr 21, 2013 at 11:14 AM 0
Share

So you mean you can't do it because you don't know the rigidbody's velocity? That would just be taking the new transform.position from the previous transform.position and dividing it by Time.deltaTime;

avatar image whydoidoit · Apr 21, 2013 at 11:24 AM 0
Share

But it's some else's script - hmmm, well rigidbodies and steps are difficult. I'll leave it to someone else.

avatar image AlucardJay · Apr 21, 2013 at 03:58 PM 0
Share

I just ran into this exact same problem. The problem comes from lines 60 and 61 :

 var targetVelocity = new Vector3(inputX * input$$anonymous$$odifyFactor, 0.0, inputY * input$$anonymous$$odifyFactor);
 targetVelocity = myTransform.TransformDirection(targetVelocity);

so what happens here is : your rigidbody is standing vertically but moving up a slope. The above lines calculate a move vector that is trying to push into the slope, not push in a direction in relation to the angle of the slope. (myTransform.TransformDirection(targetVelocity);)

I overcame this by using an empty gameObject, rotating it to the angle of the slope using this technique (Aldos solution), and then using that transform in the TransformDirection parameter. (emptyTransform.TransformDirection(targetVelocity);)

avatar image Entyro · Apr 21, 2013 at 06:45 PM 0
Share

Didn't really understand what I'm supposed to do. Should I change "myTransform.TransformDirection(targetVelocity);" to "emptyTransform.TransformDirection(targetVelocity);"?

And what do you mean about the empty game object, how would that help?

Show more comments

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

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

Player movement: Sliding problem 0 Answers

Rigidbody Character Jumps Higher With At Least 2 Collisions 3 Answers

Rigidbody collision 3 Answers

How can I stop a box sliding off a platform when the platform is moved? 4 Answers

Tweaking character movement 1 Answer


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