コード例 #1
0
ファイル: PathFollowing.cs プロジェクト: codyamies/Asteroids
 void Update() // Draw out path calculated by agent
 {
     if (path != null)
     {
         Vector3[] corners = path.corners;
         if (corners.Length > 0)
         {
             Vector3 targetPos = corners[corners.Length - 1];
             GizmosGL.color = new Color(1, 0, 0, 0.3f);
             GizmosGL.AddSphere(targetPos, targetRad * 2);
             float dist = Vector3.Distance(transform.position, targetPos);
             if (dist >= targetRad)
             {
                 GizmosGL.color = Color.cyan;
                 for (int i = 0; i < corners.Length - 1; i++)
                 {
                     Vector3 nodeA = corners[i];
                     Vector3 nodeB = corners[i + 1];
                     GizmosGL.AddLine(nodeA, nodeB, 0.1f, 0.1f);
                     GizmosGL.AddSphere(nodeB, 1f);
                     GizmosGL.color = Color.red;
                 }
             }
         }
     }
 }
コード例 #2
0
    // Move the selected piece if one is selected
    void MoveSelection()
    {
        // Check if a piece has been selected
        if (selectedPiece != null)
        {
            // Create a new ray from the camera
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            GizmosGL.color = Color.yellow;
            GizmosGL.AddLine(ray.origin, ray.origin + ray.direction * rayDistance, 0.1f, 0.1f);
            RaycastHit hit;
            // Raycast to only hit objects that aren't pieces
            if (Physics.Raycast(ray, out hit, rayDistance, ~selectionIgnoreLayer))
            {
                hitPoint = hit.point;
                // Obtain the hit point
                GizmosGL.color = Color.blue;
                GizmosGL.AddSphere(hit.point, 0.5f);
                // Move the piece to position
                Vector3 piecePos = hit.point + Vector3.up * pieceHeight;
                selectedPiece.transform.position = piecePos;
            }

            if (Input.GetMouseButtonUp(0))
            {
                // Drop the piece to hitpoint
                Piece piece = selectedPiece.GetComponent <Piece>();
                board.DropPiece(piece, hitPoint);

                // Deselect the piece
                selectedPiece = null;
            }
        }
    }
コード例 #3
0
    void CheckSelection()
    {
        // SET ray to ray from camera
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        GizmosGL.AddLine(ray.origin, ray.origin + ray.direction * rayDistance, 0.1f, 0.1f, Color.red, Color.blue);

        // SET hit to new RaycastHit
        RaycastHit hit = new RaycastHit();

        // IF Physics.Raycast() and pass ray, out hit, rayDistance, selectionLayer
        if (Physics.Raycast(ray, out hit, rayDistance, selectionLayer))
        {
            // CALL GizmoGL.AddSphere() and pass hit.point, 5f, Quaternion.identity, ay color
            GizmosGL.AddSphere(hit.point, 5f, Quaternion.identity, Color.red);
            // IF user clicked left mouse button
            if (Input.GetMouseButtonDown(0))
            {
                animator.SetFloat("moveSpeed", 1);
                // SET selectedTarget to hit.collider.transform
                selectedTarget = hit.collider.transform;
                // CALL ApplySelection
                ApplySelection();
            }
        }
    }
コード例 #4
0
 // Draws out the path the NPC follows
 void OnDrawGizmos()
 {
     // The color of the Gizmos will be red
     Gizmos.color = Color.red;
     // what to do if the path is not calculated
     if (path != null)
     {
         Vector3[] corners = path.corners;
         if (corners.Length > 0)
         {
             Vector3 targetPos = corners[corners.Length - 1];
             GizmosGL.color = new Color(1, 0, 0, 0.3f);
             GizmosGL.AddSphere(targetPos, targetRadius);
             // Calculate the distance from the agent to the target
             float distance = Vector3.Distance(transform.position, targetPos);
             // Checks if the distance is greater or equal to the target Radius
             if (distance >= targetRadius)
             {
                 Gizmos.color = Color.cyan;
                 // Draw lines between nodes
                 for (int i = 0; i < corners.Length - 1; i++)
                 {
                     Vector3 nodeA = corners[i];
                     Vector3 nodeB = corners[i + 1];
                     GizmosGL.AddLine(nodeA, nodeB, 0.1f, 0.1f);
                     GizmosGL.AddSphere(nodeB, 1f);
                     GizmosGL.color = Color.red;
                 }
             }
         }
     }
 }
コード例 #5
0
ファイル: TowerSelect.cs プロジェクト: Pyroblique/Cubithia
    // Move the selected piece if one is selected
    void MoveSelection()
    {
        // Check if a place has been selected
        if (selectedTower != null)
        {
            // Create a new ray from the camera
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            GizmosGL.color = Color.yellow;
            GizmosGL.AddLine(ray.origin, ray.origin + ray.direction * rayDistance, 0.1f, 0.1f);
            RaycastHit hit;
            Vector3    hitPoint = Vector3.zero;
            // Raycast to only hit objects that aren't towers
            if (Physics.Raycast(ray, out hit, rayDistance, ~selectionIgnoreLayer))
            {
                // Obtain the hit point
                GizmosGL.color = Color.blue;
                GizmosGL.AddSphere(hit.point, 0.5f);
                hitPoint = hit.point;
                // Move the tower to position
                Vector3 piecePos = hit.point + Vector3.up * pieceHeight;
                selectedTower.transform.position = piecePos;
            }

            // Check if mouse button is released
            if (Input.GetMouseButtonUp(0))
            {
                // Drop the piece on hitPoint
                selectedTower.transform.position = hitPoint;

                // Deselect the piece
                selectedTower = null;
            }
        }
    }
コード例 #6
0
    public override Vector3 GetForce()
    {
        Vector3 force = Vector3.zero;

        if (path != null && path.Count > 0)
        {
            Vector3 currentPosition = path[currentNode].position;
            if (Vector3.Distance(transform.position, currentPosition) <= nodeRadius)
            {
                currentNode++;
                if (currentNode <= path.Count)
                {
                    currentNode = path.Count - 1;
                }
            }
            force = Seek(currentPosition);
            #region GISMO
            Vector3 previousPosition = path[0].position;
            foreach (Node node in path)
            {
                GizmosGL.AddSphere(node.position, grap.nodeRadius, Quaternion.identity, Color.blue);
                GizmosGL.AddLine(previousPosition, node.position, 0.01f, 0.01f, Color.yellow);
                previousPosition = node.position;
            }
            #endregion
        }

        return(force);
    }
コード例 #7
0
        private void OnDrawGizmos()
        {
            Gizmos.color = Color.red;
            if (path != null)
            {
                Vector3[] corners = path.corners;
                if (corners.Length > 0)
                {
                    Vector3 targetPos = corners[corners.Length - 1];

                    // Draw target
                    Gizmos.color = Color.magenta;
                    Gizmos.DrawWireSphere(targetPos, targetRadius);

                    GizmosGL.color = new Color(1, 0, 0, 0.3f);
                    GizmosGL.AddSphere(targetPos, targetRadius);

                    float distance = Vector3.Distance(transform.position, targetPos);
                    if (distance >= targetRadius)
                    {
                        GizmosGL.color = Color.cyan;
                        for (int i = 0; i < corners.Length - 1; i++)
                        {
                            GizmosGL.AddLine(corners[i], corners[i + 1], 0.1f, 0.1f);
                            GizmosGL.AddSphere(corners[i + 1], 1f);


                            GizmosGL.color = Color.red;
                        }
                    }
                }
            }
        }
コード例 #8
0
ファイル: Seek.cs プロジェクト: codyamies/Asteroids
        public override Vector3 GetForce()
        {
            // SET force to Vector3 zero
            Vector3 force = Vector3.zero;

            // IF target is null, return force
            if (target == null)
            {
                return(force);
            }

            // SET desiredForce
            Vector3 desiredForce = target.position - transform.position;

            #region GizmosGL
            GizmosGL.color = Color.white;
            GizmosGL.AddLine(transform.position, target.position, .1f, .1f);
            GizmosGL.color = new Color(1, 0.325f, 0.305f, 1);
            GizmosGL.AddSphere(target.position, stoppingDistance * 2f);
            #endregion

            // Check if the direction is valid
            if (desiredForce.magnitude > stoppingDistance)
            {
                // Calculate force
                desiredForce = desiredForce.normalized * weighting;
                force        = desiredForce - owner.velocity;
            }

            // Return the force!
            return(force);
        }
コード例 #9
0
        void Update()
        {
            // is the path calculated?i
            if (path != null)
            {
                Vector3[] corners = path.corners;
                if (corners.Length > 0)
                {
                    Vector3 targetPos = corners[corners.Length - 1];
                    // Draw the target
                    Gizmos.color = new Color(1, 0, 0, 0.3f);

                    //. calculate distance from agent to target
                    float distance = Vector3.Distance(transform.position, targetPos);
                    // If distance is greater than the radius
                    if (distance >= targetRadius)
                    {
                        Gizmos.color = Color.cyan;
                        // Draw lines between nodes
                        for (int i = 0; i < corners.Length - 1; i++)
                        {
                            Vector3 nodeA = corners[i];
                            Vector3 nodeB = corners[i + 1];
                            GizmosGL.AddLine(nodeA, nodeB, 0.1f, 0.1f);
                            GizmosGL.AddSphere(nodeB, 1f);
                            GizmosGL.color = Color.blue;
                        }
                    }
                }
            }
        }
コード例 #10
0
        public override Vector3 GetForce()
        {
            Vector3 force       = Vector3.zero;
            Vector3 centerPoint = transform.position + rayOffset;
            Vector3 ahead       = centerPoint + transform.forward.normalized * maxSeeDistance;
            Ray     ray         = new Ray(centerPoint, transform.forward);

            //Line line = GizmosGL.AddLine(ray.origin, ray.origin + ray.direction * maxSeeDistance);
            //line.color = Color.cyan;

            RaycastHit hit;

            if (Physics.Raycast(ray, out hit, maxSeeDistance, collisionLayer))
            {
                //line.color = Color.red;
                //Vector3 center = hit.point + -hit.normal * intersectForce;
                Vector3 center    = hit.collider.transform.position;
                Vector3 direction = ahead - center;
                Vector3 avoidance = direction.normalized * maxAvoidForce;
                avoidance *= maxAvoidForce;
                Collider col    = hit.collider;
                Bounds   bounds = col.bounds;
                GizmosGL.AddCube(center, bounds.size * 2);
                GizmosGL.AddSphere(center, 0.5f);
                GizmosGL.AddLine(transform.position, transform.position + avoidance * 10f, 1, 1, Color.red, Color.red);
                Vector3 desiredVelocity = avoidance.normalized;

                force  = Vector3.zero;
                force += desiredVelocity;
            }

            return(force);
        }
コード例 #11
0
 // Draw out the path calculated by the agent
 void Update()
 {
     // Is the path calculated?
     if (path != null)
     {
         // Corners refers to the nodes that Unity generated through A*
         Vector3[] corners = path.corners;
         // Has generated corners for the path?
         if (corners.Length > 0)
         {
             // Store the last corner into target pos
             Vector3 targetPos = corners[corners.Length - 1];
             // Draw the target
             GizmosGL.color = new Color(1, 0, 0, 0.3f);
             GizmosGL.AddSphere(targetPos, targetRadius * 2f);
             // Calculate distance from agent to target
             float distance = Vector3.Distance(transform.position, targetPos);
             // Is the distance greater than target radius?
             if (distance >= targetRadius)
             {
                 GizmosGL.color = Color.cyan;
                 for (int i = 0; i < corners.Length - 1; i++)
                 {
                     Vector3 nodeA = corners[i];
                     Vector3 nodeB = corners[i + 1];
                     GizmosGL.AddLine(nodeA, nodeB, 0.1f, 0.1f);
                     GizmosGL.AddSphere(nodeB, 1f);
                     Gizmos.color = Color.red;
                 }
             }
         }
     }
 }
コード例 #12
0
 void Update()
 {
     HandleSelector();
     foreach (var p in patrolPoints)
     {
         GizmosGL.AddSphere(p.position, 1f);
     }
 }
コード例 #13
0
 // Update is called once per frame
 void FixedUpdate()
 {
     GizmosGL.AddRing(ring.position, ring.innerRadius, ring.outerRadius, Quaternion.Euler(ring.rotation), ring.scale, ring.segments, ring.color);
     GizmosGL.AddArc(arc.position, arc.radius, arc.halfAngle, arc.rotation, arc.segments, arc.color);
     GizmosGL.AddSphere(sphere.position, sphere.radius, sphere.rotation, sphere.color);
     GizmosGL.AddSquare(square.position, square.scale, square.rotation);
     GizmosGL.AddCircle(circle.position, circle.radius, Quaternion.identity, circle.segments, circle.color);
     GizmosGL.AddCube(cube.position, cube.scale, cube.rotation);
     GizmosGL.AddCube(cube.position, cube.scale, cube.rotation);
     GizmosGL.AddCylinder(cylinder.position, cylinder.radius, cylinder.scale, cylinder.rotation, cylinder.halfLength, cylinder.segments, cylinder.color);
     GizmosGL.AddLine(line.start, line.end, line.startWidth, line.endWidth, line.startColor, line.endColor);
 }
コード例 #14
0
    // Calculates force for behaviour
    public override Vector3 GetForce()
    {
        // SET force to zero
        Vector3 force = Vector3.zero;

        // IF path is not null AND path count is greater than zero
        if (path != null && path.Count > 0)
        {
            // SET currentPos to path[currentNode] position
            Vector3 currentPos = path[currentNode].position;
            // IF distance between transform's position and currentPos is less than or equal to nodeRadius
            // IF (distance between transform's position and currentPos) <= (nodeRadius)
            // IF (Vector3.Distance(transform.position, currentPos) <= (nodeRadius)

            if (Vector3.Distance(transform.position, currentPos) <= nodeRadius)
            {
                // Increment currentNode
                currentNode++;

                // IF currentNode is greater than or equal to path.count
                if (currentNode > path.Count)
                {
                    // SET currentNode to path.Count -1
                    currentNode = path.Count - 1;
                }
            }
            // SET force to Seek() and pass currentPos
            force = Seek(currentPos);

            #region GIZMOS
            // SET prevPosition to path[0].position
            Vector3 prevPosition = path[0].position;
            // FOREACH node in path
            foreach (Node node in path)
            {
                // CALL GizmoGL.AddSphere() and pass node's position, graph's nodeRadius, identity, any colour
                GizmosGL.AddSphere(node.position, graph.nodeRadius, Quaternion.identity, Color.green);
                // CALL GizmoGL.AddLine() and pass prev, node's position, 0.1f, 0.1f, any color, any color
                GizmosGL.AddLine(prevPosition, currentPos, 0.1f, 0.1f, Color.red, Color.blue);
                // SET prev to node's positio
                prevPosition = node.position;
            }
            #endregion
        }
        #endregion


        // RETURN force
        return(force);
    }
コード例 #15
0
    void CheckSelection()
    {
        Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit = new RaycastHit();

        if (Physics.Raycast(ray, out hit, rayDist, selectionLayer))
        {
            GizmosGL.AddSphere(hit.point, 5f, Quaternion.identity, Color.red);
            if (Input.GetMouseButtonDown(0))
            {
                selectedTarget = hit.collider.transform;
                ApplySelection();
            }
        }
    }
コード例 #16
0
        // Update is called once per frame
        void Update()
        {
            GizmosGL.AddCircle(circle.position, circle.up, circle.radius, circle.segments, circle.color);
            GizmosGL.AddSquare(square.position, square.up, square.scale, square.color);
            GizmosGL.AddCube(cube.position, cube.scale, cube.color);
            GizmosGL.AddSphere(sphere.position, sphere.radius, sphere.rows, sphere.columns, sphere.color, sphere.longMin, sphere.longMax, sphere.latMin, sphere.latMax);
            GizmosGL.AddCylinder(cylinder.position, cylinder.up, cylinder.radius, cylinder.halfLength, cylinder.segments, cylinder.color);
            GizmosGL.AddRing(ring.position, ring.up, ring.innerRadius, ring.outerRadius, ring.segments, ring.color);
            GizmosGL.AddArc(arc.position, arc.up, arc.radius, arc.rotation, arc.halfAngle, arc.segments, arc.color);
            GizmosGL.AddLine(line.start, line.end, line.startWidth, line.endWidth, line.startColor, line.endColor);


            for (int i = 0; i < 50; i++)
            {
                GizmosGL.AddSphere(new Vector3(Mathf.Sin(i) * 10, i * 10, Mathf.Cos(i) * 10), 1, 16, 16, Color.red);
            }
        }
コード例 #17
0
    // Calculates force for behaviour
    public override Vector3 GetForce()
    {
        // SET force to zero
        Vector3 force = Vector3.zero;

        // IF path is not null AND path Count is greater than zero
        if (path != null && path.Count > 0)
        {
            // SET currentPos to current path[currentNode] position
            Vector3 currentPos = path[currentNode].position;
            // IF distance between transform's position and currentPos is less than or equal to nodeRadius
            if (Vector3.Distance(transform.position, currentPos) <= nodeRadius)
            {
                // Increment currentNode
                currentNode++;
                // IF currentNode is greater than or equal to path.Count
                if (currentNode >= path.Count)
                {
                    // SET currentNode to path.Count - 1
                    currentNode = path.Count - 1;
                }
            }
            // SET force to Seek() and pass currentPos
            force = Seek(currentPos);
        }

        #region GIZMOS
        // SET prevposition to path[0].position
        var prevposition = path[0].position;
        // FOREACH node in path
        foreach (var node in path)
        {
            // CALL GizmosGL.AddSphere() and pass node's position, graph's nodeRadius, identity, any color
            GizmosGL.AddSphere(node.position, graph.nodeRadius, transform.rotation, Color.black);
            // CALL GizmosGL.AddLine() and pass node's position
            GizmosGL.AddLine(prevposition, node.position);
            // SET prev to node's position
            prevposition = node.position;
        }
        #endregion

        // Return force
        return(force);
    }
コード例 #18
0
 // Move the slected piece if one is selected
 void MoveSelection()
 {
     // chekc if a piece has been selected
     if (selectedPiece != null)
     {
         // Create a new ray from the camera
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         GizmosGL.color = Color.yellow;
         GizmosGL.AddLine(ray.origin, ray.origin + ray.direction * rayDistance, 0.1f, 0.1f);
         RaycastHit hit;
         // Raycast to only hit objects that aren't pieces
         if (Physics.Raycast(ray, out hit, rayDistance, ~selectionIgnoreLayer))
         {
             // Obtain the hit point
             GizmosGL.color = Color.blue;
             GizmosGL.AddSphere(hit.point, 0.5f);
         }
     }
 }
コード例 #19
0
    // Constantly checking inputs
    void CheckSelection()
    {
        // Set ray to cray from camera
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        // Set hit to new RaycastHit
        RaycastHit hit = new RaycastHit();

        // If Physics.Raycast() and pass ray, out hit, ray distance, selectionLayer
        if (Physics.Raycast(ray, out hit, rayDistance, selectionLayer))
        {
            // Call GizmosGL.AddSphere() and pass hit.point, 5f, Quaternion.identity, any color
            GizmosGL.AddSphere(hit.point, 5f, Quaternion.identity, Color.black);
            // If user clicks left mouse button
            if (Input.GetMouseButtonDown(0))
            {
                // Set selectedTarget to hit.collider.transform
                selectedTarget = hit.collider.transform;
                // Call ApplySelection()
                ApplySelection();
            }
        }
    }
コード例 #20
0
    // Constantly checking foro input
    void CheckSelection()
    {
        // SET ray to ray from Camera
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        // SET hit to new RaycastHit
        RaycastHit hit = new RaycastHit();

        // IF Physics.Raycast() and pass ray, out hit, rayDistance, selectionLayer
        if (Physics.Raycast(ray, out hit, rayDistance, selectionLayer))
        {
            // CALL GizmosGL.AddSphere() and pass hit.point, 5f, Quaternion.Identity, any color
            GizmosGL.AddSphere(hit.point, 5f, Quaternion.identity, Color.red);
            // IF user clicked left mouse button
            if (Input.GetMouseButtonDown(0))
            {
                // SET selectedTarget to hit.collider.transform
                selectedTarget = hit.collider.transform;
                // CALL ApplySelection();
                ApplySelection();
            }
        }
    }
コード例 #21
0
    void MoveSelection()
    {
        // Check if a piece has been selected
        if (selectedPiece != null)
        {
            // Create a new ray from camera
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            GizmosGL.color = Color.yellow;
            GizmosGL.AddLine(ray.origin, ray.origin + ray.direction * rayDistance);
            RaycastHit hit;
            // Raycast to only hit objects that arent piece
            if (Physics.Raycast(ray, out hit, rayDistance, ~selectionIngoreLayer))
            {
                // Obtain the hit point
                GizmosGL.color = Color.blue;
                GizmosGL.AddSphere(hit.point, 0.5f);

                Vector3 piecePos = hit.point + Vector3.up * pieceHeight;
                selectedPiece.transform.position = piecePos;
            }
        }
    }
コード例 #22
0
    // Calculates force for behaviour
    public override Vector3 GetForce()
    {
        // Set force to zero
        Vector3 force = Vector3.zero;

        // If  a path is not null AND path count is greater than zero
        if (path != null && path.Count > 0)
        {
            // Set currentPos to path[currentNode] position
            Vector3 currentPos = path[currentNode].position;
            // if distance between transfom.position and currentPos is less than or equal to nodeRadius
            if (Vector3.Distance(transform.position, currentPos) <= nodeRadius)
            {
                currentNode++;
                if (currentNode >= path.Count)
                {
                    currentNode = path.Count - 1;
                }
            }
            force = Seek(currentPos);
            #region Gizmos
            // Set previous position to path[0].position
            Vector3 previousPos = path[0].position;
            // Foreach Node in path
            foreach (Node node in path)
            {
                // Call GizmosGl.AddSphere() and pass node.position, graph.nodeRadius, identity, any color
                GizmosGL.AddSphere(node.position, graph.nodeRadius, Quaternion.identity, Color.blue);
                // Call GizmosGl.AddLine() and pass previousPos, node.position, 0.1f,0.1f, any color, any color
                GizmosGL.AddLine(previousPos, node.position, .1f, .1f, Color.red, Color.green);
                //set previousPos to node.position
                previousPos = node.position;
            }
            #endregion
        }
        // RETURN force
        return(force);
    }