- Home /
Rigidbody movement walls climb disable
Hello i have a problem. I have maked own rigidbody movement script, and it have air acceleration. So the player can jump on the walls and it can stay on the walls. How i can stop it easily. I have tried physicmaterials what have 0 friction but it didn't work. here is the script :
var speed = 10.0;
var gravity = 10.0;
var maxVelocityChange = 10.0;
var canJump = true;
var jumpHeight = 2.0;
var airspeed = 8.0;
private var truespeed = 0.0;
private var grounded = false;
@script RequireComponent(Rigidbody, CapsuleCollider)
function Awake ()
{
rigidbody.freezeRotation = true;
rigidbody.useGravity = false;
}
function FixedUpdate ()
{
// Calculate how fast we should be moving
var targetVelocity = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
targetVelocity = transform.TransformDirection(targetVelocity);
targetVelocity *= truespeed;
// 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(grounded == false){
truespeed = airspeed;
}
if (grounded == true)
{
truespeed = speed;
// Jump
if (canJump && Input.GetButton("Jump"))
{
rigidbody.velocity = Vector3(velocity.x, CalculateJumpVerticalSpeed(), velocity.z);
}
}
// We apply gravity manually for more tuning control
rigidbody.AddForce(Vector3 (0, -gravity * rigidbody.mass, 0));
grounded = false;
}
function OnCollisionStay (hit : Collision)
{
grounded = true;
if(hit.gameObject.tag == "friction"){
transform.parent = hit.transform ;
}else{
transform.parent = null;
}
}
function CalculateJumpVerticalSpeed ()
{
// From the jump height and gravity we deduce the upwards speed
// for the character to reach at the apex.
return Mathf.Sqrt(2 * jumpHeight * gravity);
}
Answer by Phoera · Aug 07, 2015 at 11:28 AM
You can make your grounded check by raycasting forward from the player instead of doing this down, and then don't apply gravity if character is grounded (wall'd actually :)
Answer by Cherno · Apr 24, 2015 at 09:56 PM
Personally, I find that creating a Physics material helps. You can also push back the character from the contact point when he hits a wall with a force equal to the movement velocity, or slightly above.
Your answer
![](https://koobas.hobune.stream/wayback/20220613193906im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Wall jump, jump movement not working 0 Answers
strange behaviour after object collision 1 Answer
How to make an object without Rigidbody move at the same speed as an object with Rigidbody? 1 Answer
How to rotate a Rigidbody without rotating the gameObject? 1 Answer
How to drag and drop a instantiate prefab on my mobile game 3D using touchs! 0 Answers