- Home /
Character - Collider With Scene
Hello,
I'm working on my own character controller, this is the first time I've ever done anything like this, so be easy with me.
I'm working on a simple top down game, where the character moves where the mouse is in the scene. I got all the movement working, thats all fine. What I'm trying to do now, is for the character to collider with objects in the scene.
So, I've added a collider to my player, and a rigidbody and turned off gravity (as I don't really need it in a top down, no jumping involved).
Now this works, I do collide, but then my character starts rolling, and rotating and moves backwards, out of control basically. So then I tried turning on some of the Constraints on the rotation, this works, my character doesn't lose control, but it seems like theres a force continually being applied against my character, causing him to move backwards.
Any ideas how I can have my character collider with the scene? Without losing control?
I'll attach my script and continue to look around, but I'm basically just using 'Translates'.
function Update () {
//makes direction arrow stay with character - on x and z axis only
playerArrow.transform.position.x = transform.position.x;
playerArrow.transform.position.z = transform.position.z;
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hitdist = 0.0;
//get the point along the ray that hits the calculated distance.
var targetPoint = ray.GetPoint(hitdist);
var lookPos = targetPoint - playerArrow.transform.position;
lookPos.y = 0;
var targetRotation = Quaternion.LookRotation(lookPos);
playerArrow.transform.rotation = Quaternion.Slerp(playerArrow.transform.rotation, targetRotation, arrowRotateSpeed * Time.deltaTime);
// ----- Move Character ----- \\
if(Input.GetAxis("Vertical")){
//rotate player to face direction arrow
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, playerRotateSpeed * Time.deltaTime);
//them move player towards arrow direction
var translation : float = Input.GetAxis("Vertical") * playerMoveSpeed;
translation *= Time.deltaTime;
transform.Translate(0,0,translation);
}
if(Input.GetAxis("Horizontal")){
var panning : float = Input.GetAxis ("Horizontal") * playerMoveSpeed;
panning *= Time.deltaTime;
transform.Translate(panning,0,0);
//pan the character left/right whilst looking at mouse position/direction arrow
if(allowRotatePan){
//rotate player to face arrow
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, playerRotateSpeed * Time.deltaTime);
}
}
}
Have you read through Unitys Char Cntlr script? That would be the first place id start. See how they implemented it, and apply the same technique or modify it to fit your needs.
The rolling around should come from the rigidbody. Try turning it off.
You could also use a character as it uses kinematics not using colliders. If nothing works you could give it a try.
Your answer
Follow this Question
Related Questions
messed up with character controller, collider and rigid body 0 Answers
Collision not working 1 Answer
Character Controller can pass through character controller after upgrade to 4.0 0 Answers
Moving Rigidbody with Translate causes vibrating player when walking into objects 2 Answers
Character Controller 2 Answers