// Update is called once per frame
    void Update()
    {
        curSpeed       = speed * Time.deltaTime;
        targetPosition = path.GetPosition(curPathIndex);

        if (Vector3.Distance(transform.position, targetPosition) < path.radius)
        {
            if (curPathIndex < pathLength - 1)
            {
                curPathIndex++;
            }
            else if (isLooping)
            {
                curPathIndex = 0;
            }
            else
            {
                return;
            }
        }

        // Vector3 dV = Accelerate(targetPosition);
        Vector3 dV = targetPosition - mRigidbody.position;

        dV.Normalize();
        dV.y        = 0;
        curVelocity = dV * 0.1f;

        // Debug.Log(dV.ToString("F5"));

        // mRigidbody.MovePosition(mRigidbody.position + curVelocity);

        Vector3 forward   = gameObject.transform.forward;
        float   turnAngle = 0f;

        turnAngle = Vector2.Angle(new Vector2(-forward.x, -forward.z), new Vector2(dV.x, dV.z));
        if (Vector3.Cross(new Vector2(-forward.x, -forward.z), new Vector2(dV.x, dV.z)).z < 0)
        {
            turnAngle = -turnAngle;
        }
        turnAngle = Mathf.Clamp(turnAngle, -180f, 180f);

        if (180f - Mathf.Abs(turnAngle) > 5 && Mathf.Abs(turnAngle) > 5)
        {
            // Debug.Log(turnAngle);
            turnAngle /= 180f;
            turnAngle *= mTurnSpeed;
            Quaternion turnRotation = Quaternion.Euler(0f, -turnAngle, 0f);
            mRigidbody.MoveRotation(mRigidbody.rotation * turnRotation);
            mTankMoveEffect.Turn(-turnAngle);
        }
        else
        {
            mTankMoveEffect.Move(curVelocity.magnitude * 10);
            mRigidbody.MovePosition(mRigidbody.position + curVelocity);
        }
    }
Exemple #2
0
    private void Move()
    {
        if (Mathf.Abs(mMovementInputValue) >= 0.1f && mSpeed <= mMinSpeed)
        {
            mSpeed = mMinSpeed + mAccSpeed;
        }
        else if (Mathf.Abs(mMovementInputValue) >= 0.1f)
        {
            mSpeed = mSpeed + mAccSpeed;
            mSpeed = Mathf.Clamp(mSpeed, mMinSpeed, mMaxSpeed);
        }
        else
        {
            mSpeed = mMinSpeed;
        }
        Vector3 moveVec = transform.forward * mMovementInputValue * (-mSpeed) * Time.deltaTime;

        mTankMoveEffect.Move(mMovementInputValue * (-mSpeed));
        mRigidbody.MovePosition(mRigidbody.position + moveVec);
    }