- Home /
Mouse Controlled Character and Colliders
Hi, im having some problems with my mouse controlled character, its going through buildings even when they have colliders. I've tried all the colliders and none prevent the character from going through. I was able to find a work around by adding a rigid body on the character and freezing position on X and Y, and freezing rotation on X, Y and Z. This way the character doesnt go through buildings but whenever I click on the position I want him to go he keeps sliding (because I didnt freeze position on Z). Any solution for my problem?
Heres a screenshot of the problem:
http://img534.imageshack.us/img534/5138/colliders.jpg
Regards, ExyloN
Not sure if this is a mistake, or you advertising for that website, but the link doesn't lead to a screenshot, but the frontpage.
This is the code that im using:
//variaveis
var vel : int;
vel = 0;
var Animacao : Animation;
//Clipes de animação
var clipes:AnimationClip[];
//Guarda se ja foi clicado
private var clicado:boolean = false;
private var seta : GameObject;
function Start()
{
seta = GameObject.CreatePrimitive(PrimitiveType.Plane);
seta.transform.position = Vector3(0, 0, 0);
}
function Update()
{
//se clicou com botão esq
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse1))
{
//Cria um plano com a posição da personagem
var playerPlane = new Plane(Vector3.up, transform.position);
//Desenha um ponto entre o personagem e o local clicado
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hitdist = 0.0;
vel = 10;
//Seta que foi clicado
clicado = true;
//Verifica se pode mover-se e desenha o ca$$anonymous$$ho
if (playerPlane.Raycast (ray, hitdist))
{
//cria variavel depois de criado o ca$$anonymous$$ho
var targetPoint = ray.GetPoint(hitdist);
//guarda a rotação
targetPosition = ray.GetPoint(hitdist);
var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
seta.transform.position = Vector3(targetPosition.x,targetPosition.y,targetPosition.z );
//Ajusta rotação da personagem
transform.rotation = targetRotation;
}
}
//Caso a personagem não esteja no ponto clicado ela anda até lá
if((transform.position - seta.transform.position).magnitude > 2)
{
transform.Translate(Vector3.forward * ( Time.deltaTime * vel));
//animação dele a andar
Animacao.clip = clipes[1];
Animacao.Play();
}
else
{
//animação dele parado
Animacao.clip = clipes[0];
Animacao.Play();
}
}
Answer by Piflik · Jul 13, 2012 at 02:49 PM
How are you moving the character? Things like 'transform.position' or 'transform.Translate()' ignore all colliders. If you want to have collisions between player and scenery, you need to use the rigidbody to move the object (e.g. rigidbody.AddForce() or rigidbody.velocity).
Your answer
Follow this Question
Related Questions
How do I make the character look at the mouse? 0 Answers
Raycast Without Colliders 6 Answers
How do i make the character look the mouse 0 Answers
c# scripting navigation problems 1 Answer
aiming with animation 1 Answer