private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.layer == 8) // obstacle
        {
            print("find obstacle");
            print("object name=" + collision.gameObject.name);
            //lives -= 1;
            Transform nearest = roadLine.getNearestPoint();
            this.transform.position = new Vector3(nearest.position.x, 0, nearest.position.z);
            gameObject.GetComponent<Rigidbody>().velocity = Vector3.zero;
            agent.OnCarHitObstacle();
            //Transform secondNearest = roadLine.getNearestPointOf(nearest);
            //Vector3 roadDirection = secondNearest.position - nearest.position;
            //Vector3 destDirection = roadLine.endPoint.position - nearest.position;
            //float angle = Vector3.Angle(destDirection.normalized ,roadDirection.normalized);
            //Vector3 lookAtVec = new Vector3(secondNearest.position.x, 0, secondNearest.position.z);

            //print("NEAR=" + nearest.name + " 2NEAR=" + secondNearest.name);

            //float dir = 1;
            //if (angle > 90) {
            //    lookAtVec *= -1;
            //    dir = -1;
            //}

            //transform.LookAt(lookAtVec);
            //float y = transform.rotation.eulerAngles.y;
            //if (y < 0) {
            //    y += 360;
            //}
            //if (y < 45)
            //{
            //    y = 0;
            //}
            //else if (y < 135) {
            //    y = 90;
            //}
            //else if (y < 225)
            //{
            //    y = 180;
            //}
            //else if (y < 315)
            //{
            //    y = 270;
            //}
            //print("y=" + y);
            ////transform.rotation = Quaternion.Euler(roadDirection.normalized * dir);
            //// transform.rotation.SetEulerAngles(Vector3.up * y);

            //gameObject.transform.eulerAngles = new Vector3(0, y* dir, 0);
            print("life=" + lives);
        }
        else {
            print("non obstacle=" + collision.gameObject.name);
        }
    }
Exemple #2
0
 private void OnCollisionEnter(Collision collision)
 {
     if (collision.gameObject.layer == 8) // obstacle
     {
         print("find obstacle");
         print("object name=" + collision.gameObject.name);
         //lives -= 1;
         Transform nearest = roadLine.getNearestPoint();
         this.transform.position = new Vector3(nearest.position.x, 0, nearest.position.z);
         gameObject.GetComponent <Rigidbody>().velocity = Vector3.zero;
         agent.OnCarHitObstacle();
         print("life=" + lives);
     }
     else
     {
         print("non obstacle=" + collision.gameObject.name);
     }
 }