Esempio n. 1
0
        private bool obstracleDetect()
        {
            Collider[] colliders = Physics.OverlapSphere(transform.position, 15);

            foreach (Collider collide in colliders)
            {
                if (collide.gameObject.tag == "collideEnv")
                {
                    //Vector3 enemyDirectionLocal = transform.InverseTransformPoint(collide.gameObject.transform.position);

                    string enemyDirectionLocal = AllHelperFunctions.whereIsObject(transform, collide.gameObject.transform);

                    if ((String.Compare(enemyDirectionLocal, "up") != 0) &&
                        (String.Compare(enemyDirectionLocal, "down") != 0))
                    {
                        if (String.Compare(enemyDirectionLocal, "left") == 0)
                        {
                            obstracleIn = 1;
                        }
                        else if (String.Compare(enemyDirectionLocal, "right") == 0)
                        {
                            obstracleIn = -1;
                        }

                        return(true);
                    }
                }
                //else obstracleIn = 0;
            }
            return(false);
        }
Esempio n. 2
0
        // Update is called once per frame
        void Update()
        {
            string targetPosition = AllHelperFunctions.whereIsObject(transform, target, false);

            if (!playerDied && enemyCarHelper.isNear(transform.position, target.position, maxDistance) && String.Compare("down", targetPosition) != 0)
            {
                rotateHolders();
                playerDied = carHelper.isDead;
            }
        }
Esempio n. 3
0
        private void FixedUpdate()
        {
            setDriving(carHelper.isDead);
            if (!playerDied)
            {
                //

                Vector3 offsetTargetPos = m_Target.position;
                Vector3 localTarget     = transform.InverseTransformPoint(offsetTargetPos);
                string  targetPosition  = AllHelperFunctions.whereIsObject(transform, enemyGun.getTarget(), false);

                //Debug.Log(targetPosition);

                if (localTarget.magnitude >= maxDistance)
                {
                    m_Driving = false;
                }
                else if ((
                             localTarget.magnitude > m_ReachTargetThreshold ||
                             localTarget.magnitude <= maxDistance) &&
                         (
                             String.Compare(targetPosition, "down") != 0
                         )
                         )
                {
                    //if ((localTarget.magnitude < m_ReachTargetThreshold - 5) || obstracleDetect())
                    //{
                    //    if (obstracleIn == 1) m_CarController.Move(steer += 0.5f, 0, -0.9f, 0);
                    //    else if (obstracleIn == -1) m_CarController.Move(steer -= 0.5f, 0, -0.9f, 0);

                    //    return;
                    //}

                    m_Driving = true;
                    //m_CarController.Move(0, accel , accel, 0);
                }

                if (m_Target == null || !m_Driving)
                {
                    // Car should not be moving,
                    // use handbrake to stop
                    m_CarController.Move(0, 0f, 0f, 0f);
                }
                else
                {
                    Vector3 fwd = transform.forward;
                    if (m_Rigidbody.velocity.magnitude > m_CarController.MaxSpeed * 0.1f)
                    {
                        fwd = m_Rigidbody.velocity;
                    }

                    float desiredSpeed = m_CarController.MaxSpeed;

                    // now it's time to decide if we should be slowing down...
                    switch (m_BrakeCondition)
                    {
                    case BrakeCondition.TargetDirectionDifference:
                    {
                        // the car will brake according to the upcoming change in direction of the target. Useful for route-based AI, slowing for corners.

                        // check out the angle of our target compared to the current direction of the car
                        float approachingCornerAngle = Vector3.Angle(m_Target.forward, fwd);

                        // also consider the current amount we're turning, multiplied up and then compared in the same way as an upcoming corner angle
                        float spinningAngle = m_Rigidbody.angularVelocity.magnitude * m_CautiousAngularVelocityFactor;

                        // if it's different to our current angle, we need to be cautious (i.e. slow down) a certain amount
                        float cautiousnessRequired = Mathf.InverseLerp(0, m_CautiousMaxAngle,
                                                                       Mathf.Max(spinningAngle,
                                                                                 approachingCornerAngle));
                        desiredSpeed = Mathf.Lerp(m_CarController.MaxSpeed, m_CarController.MaxSpeed * m_CautiousSpeedFactor,
                                                  cautiousnessRequired);
                        break;
                    }

                    case BrakeCondition.TargetDistance:
                    {
                        // the car will brake as it approaches its target, regardless of the target's direction. Useful if you want the car to
                        // head for a stationary target and come to rest when it arrives there.

                        // check out the distance to target
                        Vector3 delta = m_Target.position - transform.position;
                        float   distanceCautiousFactor = Mathf.InverseLerp(m_CautiousMaxDistance, 0, delta.magnitude);

                        // also consider the current amount we're turning, multiplied up and then compared in the same way as an upcoming corner angle
                        float spinningAngle = m_Rigidbody.angularVelocity.magnitude * m_CautiousAngularVelocityFactor;

                        // if it's different to our current angle, we need to be cautious (i.e. slow down) a certain amount
                        float cautiousnessRequired = Mathf.Max(
                            Mathf.InverseLerp(0, m_CautiousMaxAngle, spinningAngle), distanceCautiousFactor);
                        desiredSpeed = Mathf.Lerp(m_CarController.MaxSpeed, m_CarController.MaxSpeed * m_CautiousSpeedFactor,
                                                  cautiousnessRequired);
                        break;
                    }

                    case BrakeCondition.NeverBrake:
                        break;
                    }

                    // Evasive action due to collision with other cars:

                    // our target position starts off as the 'real' target position
                    //Vector3 offsetTargetPos = m_Target.position;

                    // if are we currently taking evasive action to prevent being stuck against another car:
                    if (Time.time < m_AvoidOtherCarTime)
                    {
                        // slow down if necessary (if we were behind the other car when collision occured)
                        desiredSpeed *= m_AvoidOtherCarSlowdown;

                        // and veer towards the side of our path-to-target that is away from the other car
                        offsetTargetPos += m_Target.right * m_AvoidPathOffset;
                    }
                    else
                    {
                        // no need for evasive action, we can just wander across the path-to-target in a random way,
                        // which can help prevent AI from seeming too uniform and robotic in their driving
                        offsetTargetPos += m_Target.right *
                                           (Mathf.PerlinNoise(Time.time * m_LateralWanderSpeed, m_RandomPerlin) * 2 - 1) *
                                           m_LateralWanderDistance;
                    }

                    // use different sensitivity depending on whether accelerating or braking:
                    float accelBrakeSensitivity = (desiredSpeed < m_CarController.CurrentSpeed)
                                                      ? m_BrakeSensitivity
                                                      : m_AccelSensitivity;

                    // decide the actual amount of accel/brake input to achieve desired speed.
                    accel = Mathf.Clamp((desiredSpeed - m_CarController.CurrentSpeed) * accelBrakeSensitivity, -1, 1);

                    // add acceleration 'wander', which also prevents AI from seeming too uniform and robotic in their driving
                    // i.e. increasing the accel wander amount can introduce jostling and bumps between AI cars in a race
                    accel *= (1 - m_AccelWanderAmount) +
                             (Mathf.PerlinNoise(Time.time * m_AccelWanderSpeed, m_RandomPerlin) * m_AccelWanderAmount);

                    // calculate the local-relative position of the target, to steer towards
                    //Vector3 localTarget = transform.InverseTransformPoint(offsetTargetPos);

                    // work out the local angle towards the target
                    float targetAngle = Mathf.Atan2(localTarget.x, localTarget.z) * Mathf.Rad2Deg;

                    // get the amount of steering needed to aim the car towards the target
                    steer = Mathf.Clamp(targetAngle * m_SteerSensitivity, -1, 1) * Mathf.Sign(m_CarController.CurrentSpeed);

                    // feed input to the car controller.

                    if ((localTarget.magnitude < m_ReachTargetThreshold - 5) || obstracleDetect())
                    {
                        if (obstracleIn == 1)
                        {
                            steer += 0.9f;
                        }
                        else if (obstracleIn == -1)
                        {
                            steer -= 0.9f;
                        }
                        accel -= 0.7f;
                    }
                    m_CarController.Move(steer, accel * 2, accel, 0f);

                    // if appropriate, stop driving when we're close enough to the target.
                    //if (m_StopWhenTargetReached && localTarget.magnitude < m_ReachTargetThreshold)
                    //if ( localTarget.magnitude < m_ReachTargetThreshold)
                    //{
                    //    //m_Driving = false;
                    //    m_CarController.Move(0, 0, 0, 0.7f);
                    //}
                    //else if( (localTarget.magnitude > m_ReachTargetThreshold) && (Vector3.Distance(transform.position, m_Target.position) < 20f) )
                    //{
                    //    m_CarController.Move(steer, accel * 2, accel, 0f);
                    //}
                }
            }
            else
            {
                stopCarAi();
            }
        }