- Home /
Question by
cyoder · Mar 18, 2013 at 08:12 PM ·
rigidbodywallsrigidbody-collisioncharactermotorsticky
Rigidbody controller with sticky walls
I'm trying to script a player controller that uses a rigidbody. Unfortunately, when moving against a wall, the force makes the rigidbody stick to the wall. I want smooth movement against them, so that it's not choppy. My first attempt involves adding the normal of the contact point to the movement vector to counteract movement into the wall, but it's still choppy. I also tried some failed experiments with Vector3.OrthoNormalize. Is there another method that is simpler and doesn't involve "brute forcing" it by specifically adding code for every wall angle scenario? (I apologize if this is an obvious/newbie question; I am a noob)
#pragma strict
// All the variables declared up here
// I cut out a fair amount of code that doesn't pertain to the problem
function Start ()
{
// set initial rigidbody settings - no gravity
rigidbody.useGravity = false;
rigidbody.freezeRotation = true;
}
function Update ()
{
if(isEnabled)
{
// input axis variables
jumpAxis = Input.GetAxis("Jump");
horizontalAxis = Input.GetAxis("Horizontal");
verticalAxis = Input.GetAxis("Vertical");
}
}
function LateUpdate ()
{
if (isEnabled)
{
// jumping logic
// movement input recieved?
if(horizontalAxis != 0 || verticalAxis != 0)
isMoving = true;
else
isMoving = false;
}
}
function FixedUpdate ()
{
if (isEnabled)
{
// initialize variables
ownerPosition = transform.position;
ownerUp = transform.up;
ownerPositionY = transform.position.y;
ownerVerticalSpeed = rigidbody.velocity.y;
ownerGravity = Physics.gravity;
// set movedirection vector
moveDirection = Vector3(horizontalAxis, 0, verticalAxis);
// normalize move vector
var vectMag = moveDirection.magnitude;
if(vectMag > 1)
moveDirection = moveDirection.normalized;
// Counteract sticky walls
if(isTouching)
{
var touchingNormal = contactnormalSide.normalized;
moveDirection += touchingNormal;
// Grounded/jumping/falling logic
// Apply movement and gravity
moveDirection = moveDirection * moveSpeed * Time.fixedDeltaTime;
rigidbody.velocity = Vector3(moveDirection.x, ownerVerticalSpeed, moveDirection.z);
rigidbody.AddForce(ownerGravity * rigidbody.mass);
}
}
function OnCollisionExit ()
{
// Set all the bools
}
function OnCollisionStay (collision : Collision)
{
var targetUp = transform.up;
for (var contact : ContactPoint in collision.contacts)
{
var collisionNormal = contact.normal;
var dotprod = Vector3.Dot(collisionNormal, targetUp);
if(dotprod > 0.5)
{
// set bools for grounded
contactpointBelow = contact.point;
contactnormalBelow = contact.normal;
}
else if(dotprod >= -0.5)
{
contactSide = true;
isTouching = true;
contactpointSide = contact.point;
contactnormalSide = contact.normal;
// I tried adding opposite force at the point of contact, but it was still choppy
//if (!isGrounded)
//{
// rigidbody.AddForceAtPosition(-rigidbody.velocity, contact.point);
//}
}
else
{
// set above bools
contactpointAbove = contact.point;
contactnormalAbove = contact.normal;
}
}
}
Comment