1
\$\begingroup\$

My player object containing a rigidbody that is controlled using Input, is not colliding with an enemy unless keys are pressed. When no keys are pressed, the enemy simply goes through the player without collision. I'm using Unity 5.6.1f1.

I figured the problem is in the Patrol class because when I don't attach the script to the enemy object the collision appears to take place fine. But I don't know what I'm doing wrong in this script because I seems to do it's task well. I wrote this script to make the enemy object move between points in patrolPoints which are just some empty's in the game world.

So, I would like to know if I'm doing it right or is there another way to do it.

Patrol.cs

public class Patrol : MonoBehaviour {
    public Transform[] patrolPoints;
    public float speed;
    private int currentPoint;
    private bool forward;

    // Use this for initialization
    void Start () {
        transform.position = patrolPoints [0].position;
        currentPoint = 0;
        forward = true;
    }

    // Update is called once per frame
    void Update () {
        if (transform.position == patrolPoints [currentPoint].position) {
            if (currentPoint < patrolPoints.Length - 1 && forward) {
                currentPoint++;
            } else {
                currentPoint--;
                forward = false;
            }

            if (currentPoint == 0)
                forward = true;
        }   

        transform.position = Vector3.MoveTowards (transform.position, 
            patrolPoints [currentPoint].position, speed * Time.deltaTime);
    }
}
\$\endgroup\$
13
  • \$\begingroup\$ I've used the default Input settings.... \$\endgroup\$ Commented May 31, 2017 at 13:03
  • \$\begingroup\$ @Gnemlock I've added my codes.. \$\endgroup\$ Commented May 31, 2017 at 13:11
  • \$\begingroup\$ You're sure you have no other behaviours running that might read keyboard? \$\endgroup\$
    – Engineer
    Commented May 31, 2017 at 13:16
  • 1
    \$\begingroup\$ The fact that we are having to enter into discussion like this points to why we require a minimal, verifiable example of the problem to consider debugging questions ontopic. \$\endgroup\$
    – Gnemlock
    Commented May 31, 2017 at 13:37
  • 1
    \$\begingroup\$ @Gnemlock While you are correct in saying so, one must try to appreciate that users who come here with Unity problems cannot always provide all the information till they are asked for it... heck, it's hard even for users writing applications that are fully represented as text. Let alone where a lot of the data is by default stored as part of the project metadata. \$\endgroup\$
    – Engineer
    Commented May 31, 2017 at 13:44

1 Answer 1

2
\$\begingroup\$

Move your bodies around using Rigidbody.AddForce, by modifying Rigidbody.velocity vector, or using Rigidbody.MovePosition. These should ensure that collisions continue to work, whereas your line

transform.position = Vector3.MoveTowards (transform.position, patrolPoints [currentPoint].position, speed * Time.deltaTime);

will break collision functionality.

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .