Example #1
0
        /// <summary>
        ///
        /// </summary>
        private void CreateGrid()
        {
            mGrid = new JCS_PfNode[mGridSizeX, mGridSizeY];

            Vector3 worldBottomLeft =
                transform.position -
                Vector3.right *
                mGridiWorldSize.x / 2 -
                JCS_Utility.VectorDirection(mDirection) *
                mGridiWorldSize.y / 2;

            for (int x = 0;
                 x < mGridSizeX;
                 ++x)
            {
                for (int y = 0;
                     y < mGridSizeY;
                     ++y)
                {
                    Vector3 worldPoint =
                        worldBottomLeft +
                        Vector3.right *
                        (x * mNodeDiameter + mNodeRadius) +
                        JCS_Utility.VectorDirection(mDirection) *
                        (y * mNodeDiameter + mNodeRadius);

                    bool walkable = !(Physics.CheckSphere(worldPoint, mNodeRadius, mUnwalkableMask));
                    mGrid[x, y] = new JCS_PfNode(walkable, worldPoint, x, y);
                }
            }
        }
        /// <summary>
        /// Source: http://wiki.unity3d.com/index.php?title=LookAtMouse
        /// </summary>
        private void LookAtMouse()
        {
            float speed = 10;

            Vector3 direction = JCS_Utility.VectorDirection(mDirection);

            // Generate a plane that intersects the transform's position with an upwards normal.
            Plane playerPlane = new Plane(direction, mShootAction.SpawnPoint.position);

            // Generate a ray from the cursor position
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            // Determine the point where the cursor ray intersects the plane.
            // This will be the point that the object must look towards to be looking at the mouse.
            // Raycasting to a Plane object only gives us a distance, so we'll have to take the distance,
            //   then find the point along that ray that meets that distance.  This will be the point
            //   to look at.
            float hitdist = 0.0f;

            // If the ray is parallel to the plane, Raycast will return false.
            if (playerPlane.Raycast(ray, out hitdist))
            {
                // Get the point along the ray that hits the calculated distance.
                Vector3 targetPoint = ray.GetPoint(hitdist);

                // Determine the target rotation.  This is the rotation if the transform looks at the target point.
                Quaternion targetRotation = Quaternion.LookRotation(targetPoint - mShootAction.SpawnPoint.position);

                // Smoothly rotate towards the target point.
                mShootAction.SpawnPoint.rotation = Quaternion.Slerp(mShootAction.SpawnPoint.rotation, targetRotation, speed * Time.deltaTime);
            }
        }
Example #3
0
        /// <summary>
        /// Do the look at algorithm here...
        /// </summary>
        private void DoLookAt()
        {
            // check if the effect and target avaliable?
            if (mTargetTransform == null || !mLookAction)
            {
                return;
            }

            // record down the euler angle before we changes.
            if (mLocalEulerAngles)
            {
                mLastEulerAngles = this.transform.localEulerAngles;
            }
            else
            {
                mLastEulerAngles = this.transform.eulerAngles;
            }

            Vector3 lookPoint = mTargetTransform.position;
            Vector3 direction = Vector3.up;

            // get direction according to the type.
            direction = JCS_Utility.VectorDirection(mLookDirection);

            transform.LookAt(lookPoint, direction * (int)mState);

            // apply offset angle
            if (mLocalEulerAngles)
            {
                this.transform.localEulerAngles += mAngleOffset;
            }
            else
            {
                this.transform.eulerAngles += mAngleOffset;
            }

            // TODO(JenChieh): study the rotation going on in
            //                Unity lower level archietecture.
            // rotate back to X-axis.
            if (mRotateBack90)
            {
                transform.Rotate(0, -90, 0);
            }
        }
Example #4
0
        /// <summary>
        /// Set on top of the box above the ray.
        /// </summary>
        /// <param name="cap"></param>
        /// <param name="maxDistance"></param>
        public static bool SetOnTopOfClosestBoxWithRay(
            CharacterController cap,
            float maxDistance,
            JCS_Vector3Direction inDirection)
        {
            Vector3 direction = cap.transform.TransformDirection(JCS_Utility.VectorDirection(inDirection));

            RaycastHit[] hits = Physics.RaycastAll(cap.transform.position, direction, maxDistance);

            /* Nothing detected. */
            if (hits.Length == 0)
            {
                return(false);
            }

            foreach (RaycastHit hit in hits)
            {
                /* Check if there are box collider on hit. */
                BoxCollider boxCollider = hit.transform.GetComponent <BoxCollider>();
                if (boxCollider == null)
                {
                    continue;
                }

                /* Check ray ignore attached. */
                JCS_RayIgnore ri = hit.transform.GetComponent <JCS_RayIgnore>();
                if (ri != null)
                {
                    continue;
                }

                // set on top of that box
                SetOnTopOfBox(cap, boxCollider);

                return(true);
            }

            return(false);
        }