- Home /
Click To Move Character Walks Straight Through Objects
Hey there. I currently have a working code that allows me to click anywhere on a plane that then moves my character to that position. The problem is, the character walks through all the objects there! I have tried normal colliders, but that doesn't work, but when I put a rigid body on the object the character can not walk through it per say, but instead moves it out of the way. If I freeze the positions X, Y and Z of the box through rigidbody, my character just walks through it again. Is there any code I can use so that the character cannot walk through the object but also so that the object does not move when hit?
var smooth:int; // Determines how quickly object moves towards position
private var targetPosition:Vector3;
var speed = 10;
function Update () {
if(Input.GetKeyDown(KeyCode.Mouse0))
{
smooth=0;
var playerPlane = new Plane(Vector3.up, transform.position);
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hitdist = 0.0;
if (playerPlane.Raycast (ray, hitdist)) {
var targetPoint = ray.GetPoint(hitdist);
targetPosition = ray.GetPoint(hitdist);
var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
transform.rotation = targetRotation;
}
}
var dir:Vector3 = targetPosition - transform.position;
var dist:float = dir.magnitude;
var move:float = speed * Time.deltaTime;
if(dist > move){
transform.position += dir.normalized * move;
} else {
transform.position = targetPosition;
}
transform.position += (targetPosition - transform.position).normalized * speed * Time.deltaTime;
}
Your answer
Follow this Question
Related Questions
Is Triggered Collider Issue 1 Answer
OnCollisionEnter isn't working between two RigidBodies 1 Answer
Unity 2D Trigger Detection is not working. 6 Answers
How does object shape influence its collision? 3 Answers
Rigidbody not sleeping 1 Answer