- Home /
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...
Use a CharacterController - add a Rigidbody to it and make the rigidbody is$$anonymous$$inematic = true
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;
But it's some else's script - hmmm, well rigidbodies and steps are difficult. I'll leave it to someone else.
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);)
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?
Your answer
Follow this Question
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