- Home /
Turning a rigidbody controller into a character controller (almost)
Hi, in my game I am using a rididbody controller instead of a character controller for some reasons, the thing is that the way a rigidbody contrller interacts with other objects is completily different, I prefer my rigidbody controller compared to my character controller, but at the same time I feel that the rigidbody controller has too much physics in it, for example it bounces alot when it goes against objects, I want it to basically have no interaction with other objects, but still be a rigidbody, anyone can tell me a good way to make a rigidbody kinda physicless and not so bouncy (I already have a custom physic material that has everything set to 0, even friction, maybe I should tune up the friction a little bit, but that way sometimes it gets like too slow when colliding with walls)
Answer by Raiden-Freeman · Dec 07, 2013 at 12:40 AM
I think it would be better if you kept it as a rigidbody and made it kinematic, so that other objects interact with it, but the character controller overrides the physics. How do you move the character? By applying force?
You can also see how they did it in many Unity examples (by Unity themselves), as most of their characters are rigidbodies (even in the 2d example).
setting it as kinematic won't let it move, this is my movement script:
using UnityEngine;
using System.Collections;
public class Player$$anonymous$$ovementScript : $$anonymous$$onoBehaviour {
public float $$anonymous$$ovement;
float walkAcceleration = 1600f;
public float walkAccelAirRatio = 0f;
public float walkDeAcceleration = 0.3f;
[HideInInspector]
public float walkDeAccelerationVolx;
[HideInInspector]
public float walkDeAccelerationVolz;
public int accelerationController = 1;
public GameObject cameraObject;
float maxWalkSpeed = 10f;
public Vector2 horizontal$$anonymous$$ovement;
static bool grounded = false;
public float maxSlope = 60f;
void FixedUpdate (){
horizontal$$anonymous$$ovement = new Vector2(rigidbody.velocity.x, rigidbody.velocity.z);
if(horizontal$$anonymous$$ovement.magnitude > maxWalkSpeed){
horizontal$$anonymous$$ovement = horizontal$$anonymous$$ovement.normalized;
horizontal$$anonymous$$ovement *= maxWalkSpeed;
}
rigidbody.velocity = new Vector3(horizontal$$anonymous$$ovement.x,rigidbody.velocity.y,horizontal$$anonymous$$ovement.y);
if(grounded){
float temp1x = $$anonymous$$athf.SmoothDamp(rigidbody.velocity.x, 0, ref walkDeAccelerationVolx, walkDeAcceleration);
float temp2z = $$anonymous$$athf.SmoothDamp(rigidbody.velocity.z, 0, ref walkDeAccelerationVolz, walkDeAcceleration);
rigidbody.velocity = new Vector3 (temp1x,rigidbody.velocity.y, temp2z);
}
transform.rotation = Quaternion.Euler(0f, cameraObject.GetComponent<$$anonymous$$ouseLookScript>().currentYRotation, 0f);
if (grounded)
rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * Time.deltaTime, 0, Input.GetAxis("Vertical") * walkAcceleration * Time.deltaTime);
else
rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * walkAccelAirRatio * Time.deltaTime, 0, Input.GetAxis("Vertical") * walkAcceleration * walkAccelAirRatio* Time.deltaTime);
}
void OnCollisionStay (Collision collision)
{
foreach (ContactPoint contact in collision.contacts){
if (Vector3.Angle(contact.normal, Vector3.up) < maxSlope)
grounded = true;
}
}
void OnCollisionExit (){
grounded = false;
}
}
http://docs.unity3d.com/Documentation/Components/class-Rigidbody.html At the end of the page is what I meant. Setting it to kinematic and moving it with
this.transform.translate(Vector3.up
or this.transform.translate(new Vector3(displacementInXAxis,displacementInYAxis,displacementInZAxis ...
If you want to keep using forces, you can I guess. Is your wall's* material bounciness set to 0? Also make sure you're altering the bounciness of the Collider component's material, not Rigidbody's.
*by wall I mean whatever object your character collides with and is bouncing back when you don't want him to.
In my player physics material I already have bounce combine set to $$anonymous$$imum, shouldn't that do the trick already? Since the $$anonymous$$imum value is 0 (player's bounciness)
Your answer
Follow this Question
Related Questions
Click to move a character in a terrain 1 Answer
How to let a GameObject generate force but not be affected by certain forces? 0 Answers
How not to make a RigidBody not go into ground when tilted foward? 1 Answer
Guidelines for using rigidbody, collider, CharacterControllerScript, etc? 3 Answers
Character Controller meets Rigidbody 1 Answer