- Home /
Prevent a Character Model to pass through another object
I have a character model with a rigid body attached to it. How can I prevent this model passing through other objects in the scene. FYI, I'm moving the object by altering it's transform. Also the movement of the model is influenced by mouse click, somewhat like LOL or DOTA.
Thanks in advance.
Answer by SunnySunshine · Sep 15, 2013 at 06:18 PM
You should add colliders to it.
If it's a character you're controlling, you should drop the rigid body and use the character controller: http://docs.unity3d.com/Documentation/Components/class-CharacterController.html
You can move it with collision detection using Move: http://docs.unity3d.com/Documentation/ScriptReference/CharacterController.Move.html
Something like this:
void Move()
{
Vector3 dir = new Vector3(target.position.x, transform.position.y, target.position.z) - transform.position;
float dist = dir.magnitude;
float move = speed * Time.deltaTime;
if(dist > move)
cc.Move(dir.normalized * move); // cc is reference to the character controller
else
cc.Move(dir);
}
You can download an example package I made here:
One problem still persist. I'm moving the character by clicking on the terrain (just like LOL and DOTA). I tried to add a character controller, it didn't work. Here's my code
void $$anonymous$$oveTo() //function for moving
{
Vector3 dir = targetPosition - transform.position;
float dist = dir.magnitude;
float move = speed * Time.deltaTime;
if(dist > move)
{
transform.position += dir.normalized * move;
}
else
{
transform.position = targetPosition;
anim.SetFloat(hash.moveSpeedFloat, 0f); // sets the speed variable in the animator
}
//$$anonymous$$ovement
transform.position += (targetPosition - transform.position).normalized * speed * Time.deltaTime;
//Rotation Smooth
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, Time.deltaTime * rotateSpeed);
}
You see, you need to use the move function. I updated the answer with code and a unity package example that you can download here http://speedy.sh/XNZdT/move.unitypackage . Just import it and run the demo scene.
If you want true dota movement, you need to make use of a path finding algorithm too. This is no trivial task if you're not used to program$$anonymous$$g. It might be a good idea to make use of pre-existing rts-controllers. I'm sure there are some in the asset store. Good luck.
I'm actually a C# programmer mainly for business solution softwares. I'm fairly new into game program$$anonymous$$g, but kind of getting a hold on it. Thank BTW.
Your answer
Follow this Question
Related Questions
,Roll a ball - project 0 Answers
How to draw an arrow from fps contoller 1 Answer
getting high score of game from asp.net page 2 Answers
How to get rid of the input selection screen at beginning of game 1 Answer
3d UI Using plane For all screens 0 Answers