Ejemplo n.º 1
0
        // Update is called once per frame
        void Update()
        {
            // Get pressed world position
            bool mouseDown = Input.GetMouseButtonDown(0);
            bool touchUp   = Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended;

            if (mouseDown || touchUp)
            {
                Ray   ray      = Camera.main.ScreenPointToRay(mouseDown ? Input.mousePosition : new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y));
                Plane hPlane   = new Plane(Vector3.forward, Vector3.zero);
                float distance = 0;
                if (hPlane.Raycast(ray, out distance))
                {
                    // get the hit point:
                    m_pathFindingBehaviour.TargetPos = ray.GetPoint(distance);
                }
            }
            Vector3 vTarget = m_pathFindingBehaviour.TargetPos; vTarget.z = transform.position.z;

            // stop when target position has been reached
            Vector3 vDist = (vTarget - transform.position);

            //Debug.DrawLine(vTarget, transform.position); //TODO: the target is the touch position, not the target tile center. Fix this to go to target position once in the target tile
            m_pathFindingBehaviour.enabled = vDist.magnitude > MinDistToReachTarget;
            if (!m_pathFindingBehaviour.enabled)
            {
                m_moving.Veloc = Vector3.zero;
            }

            //+++avoid obstacles
            Vector3 vTurnVel = Vector3.zero;

            if (0 != (m_phyChar.CollFlags & PhysicCharBehaviour.eCollFlags.RIGHT))
            {
                vTurnVel.x = -m_moving.MaxSpeed;
            }
            else if (0 != (m_phyChar.CollFlags & PhysicCharBehaviour.eCollFlags.LEFT))
            {
                vTurnVel.x = m_moving.MaxSpeed;
            }
            if (0 != (m_phyChar.CollFlags & PhysicCharBehaviour.eCollFlags.DOWN))
            {
                vTurnVel.y = m_moving.MaxSpeed;
            }
            else if (0 != (m_phyChar.CollFlags & PhysicCharBehaviour.eCollFlags.UP))
            {
                vTurnVel.y = -m_moving.MaxSpeed;
            }
            if (vTurnVel != Vector3.zero)
            {
                m_moving.ApplyForce(vTurnVel - m_moving.Veloc);
            }
            //---

            UpdateAnimDir();
        }
Ejemplo n.º 2
0
        // Update is called once per frame
        void Update()
        {
            m_pathFindingBehaviour.TargetPos = m_player.transform.position;
            Vector3 vTarget = m_player.transform.position; vTarget.z = transform.position.z;

            // stop folling the path when closed enough to target
            m_pathFindingBehaviour.enabled = (vTarget - transform.position).magnitude > MinDistToReachTarget;
            if (!m_pathFindingBehaviour.enabled)
            {
                m_fAngOff += Random.Range(-AngRandOff, AngRandOff);
                Vector3 vOffset = Quaternion.AngleAxis(m_fAngOff, Vector3.forward) * (AngRandRadious * Vector3.right);
                vTarget += vOffset;
                Debug.DrawLine(transform.position, m_player.transform.position, Color.blue);
                Debug.DrawRay(m_player.transform.position, vOffset, Color.blue);

                m_moving.Arrive(vTarget);
            }

            //+++avoid obstacles
            Vector3 vTurnVel = Vector3.zero;

            if (0 != (m_phyChar.CollFlags & PhysicCharBehaviour.eCollFlags.RIGHT))
            {
                vTurnVel.x = -m_moving.MaxSpeed;
            }
            else if (0 != (m_phyChar.CollFlags & PhysicCharBehaviour.eCollFlags.LEFT))
            {
                vTurnVel.x = m_moving.MaxSpeed;
            }
            if (0 != (m_phyChar.CollFlags & PhysicCharBehaviour.eCollFlags.DOWN))
            {
                vTurnVel.y = m_moving.MaxSpeed;
            }
            else if (0 != (m_phyChar.CollFlags & PhysicCharBehaviour.eCollFlags.UP))
            {
                vTurnVel.y = -m_moving.MaxSpeed;
            }
            if (vTurnVel != Vector3.zero)
            {
                m_moving.ApplyForce(vTurnVel - m_moving.Veloc);
            }
            //---

            //fix to avoid flickering of the creature when collides with wall
            if (Time.frameCount % 16 == 0)
            //---
            {
                if (!LockAnimDir)
                {
                    UpdateAnimDir();
                }
            }
        }
Ejemplo n.º 3
0
        // Update is called once per frame
        void Update()
        {
            if (m_timerGodMode > 0)
            {
                m_timerGodMode   -= Time.deltaTime;
                m_sprRender.color = Time.frameCount % 2 == 0? Color.red : Color.white;

                if (m_movingBehaviour != null)
                {
                    m_movingBehaviour.ApplyForce(m_lastDamageData.Force * m_movingBehaviour.MaxForce);
                }
            }
            else
            {
                m_sprRender.color = Color.white;
            }
        }
Ejemplo n.º 4
0
        // Update is called once per frame
        void Update()
        {
            m_pathFindingBehaviour.TargetPos = m_player.transform.position;
            Vector3 vTarget = m_player.transform.position; vTarget.z = transform.position.z;

            Ray2D sightRay     = new Ray2D(transform.position, vTarget - transform.position);
            float distToTarget = (vTarget - transform.position).magnitude;

            float fSightBlockedDist = IsSightBlockedByBlockedTiles? RpgMapHelper.Raycast(sightRay, distToTarget) : -1f;

            // NOTE: fSightBlockedDist will be -1f if sight line is not blocked by blocked collision tile
            if (distToTarget >= SightDistance || fSightBlockedDist >= 0f)
            {
                // Move around
                m_pathFindingBehaviour.enabled = false;
                vTarget    = transform.position;
                m_fAngOff += Random.Range(-AngRandOff, AngRandOff);
                Vector3 vOffset = Quaternion.AngleAxis(m_fAngOff, Vector3.forward) * (AngRandRadious * Vector3.right);
                vTarget += vOffset;
                m_moving.Arrive(vTarget);
            }
            else // Follow the player
            {
                // stop following the path when closed enough to target
                m_pathFindingBehaviour.enabled = (vTarget - transform.position).magnitude > MinDistToReachTarget;
                if (!m_pathFindingBehaviour.enabled)
                {
                    m_fAngOff += Random.Range(-AngRandOff, AngRandOff);
                    Vector3 vOffset = Quaternion.AngleAxis(m_fAngOff, Vector3.forward) * (AngRandRadious * Vector3.right);
                    vTarget += vOffset;
                    Debug.DrawLine(transform.position, m_player.transform.position, Color.blue);
                    Debug.DrawRay(m_player.transform.position, vOffset, Color.blue);

                    m_moving.Arrive(vTarget);
                }
            }

            //+++avoid obstacles
            Vector3 vTurnVel = Vector3.zero;

            if (0 != (m_phyChar.CollFlags & PhysicCharBehaviour.eCollFlags.RIGHT))
            {
                vTurnVel.x = -m_moving.MaxSpeed;
            }
            else if (0 != (m_phyChar.CollFlags & PhysicCharBehaviour.eCollFlags.LEFT))
            {
                vTurnVel.x = m_moving.MaxSpeed;
            }
            if (0 != (m_phyChar.CollFlags & PhysicCharBehaviour.eCollFlags.DOWN))
            {
                vTurnVel.y = m_moving.MaxSpeed;
            }
            else if (0 != (m_phyChar.CollFlags & PhysicCharBehaviour.eCollFlags.UP))
            {
                vTurnVel.y = -m_moving.MaxSpeed;
            }
            if (vTurnVel != Vector3.zero)
            {
                m_moving.ApplyForce(vTurnVel - m_moving.Veloc);
            }
            //---

            //fix to avoid flickering of the creature when collides with wall
            if (Time.frameCount % 16 == 0)
            //---
            {
                if (!LockAnimDir)
                {
                    UpdateAnimDir();
                }
            }
        }