Search Unity

Objects following path when selected, not following new when deselected and selected again

Discussion in 'Scripting' started by Bogaland, Jul 10, 2018.

  1. Bogaland

    Bogaland

    Joined:
    Feb 19, 2017
    Posts:
    32
    Hello!

    I've made some small missile follow a path made by the mouse when it's selected (bool selected = true). I am able to deselect the current missile (since I have multiple missiles) which turns the bool deselected = true and selected = false.

    When I reselect my missile I want the old path to be ignored and it to follow the new path made by my mouse. I figured I should just clear my list of points (linePoints) and reset the current counter (int current), but it's not working correctly. The missile just stops at the position I selected it at.

    I've attached the code which controls the missiles below, it's the entire class for the object:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class NewRotate : MonoBehaviour
    6. {
    7.  
    8.     public float speed = 0.01f;
    9.     public float rotationSpeed = 10f;
    10.     public float moveSpeed = 0.001f;
    11.     public GameObject testPos;
    12.     public GameObject explosion;
    13.     public static bool spawned;
    14.     public bool selected;
    15.     private Plane ground;
    16.     private float numPoints = 50;
    17.     private bool launched = false;
    18.     private bool hasDetected = false;
    19.     private double dist;
    20.     private GameObject missileTarget;
    21.     private bool initiated;
    22.     private bool deselected = false;
    23.     public bool drawGizmos;
    24.     private int current;
    25.     public GameObject prefab;
    26.     public BoxCollider hitBox;
    27.     List<Vector3> linePoints = new List<Vector3>();
    28.     LineRenderer lineRenderer;
    29.     public float startWidth = 1.0f;
    30.     public float endWidth = 1.0f;
    31.     public float threshold = 0.001f;
    32.     Camera thisCamera;
    33.     int lineCount = 0;
    34.  
    35.     Vector3 lastPos = Vector3.one * float.MaxValue;
    36.  
    37.     void Awake()
    38.     {
    39.         thisCamera = Camera.main;
    40.         lineRenderer = GetComponent<LineRenderer>();
    41.     }
    42.  
    43.     private void Start()
    44.     {
    45.         drawGizmos = true;
    46.         initiated = false;
    47.         spawned = true;
    48.         launched = false;
    49.         hasDetected = false;
    50.         selected = true;
    51.         Physics.IgnoreCollision(GameObject.Find("HitBox").GetComponent<BoxCollider>(), GetComponent<Collider>());
    52.     }
    53.  
    54.     void Update()
    55.     {
    56.         if (!launched)
    57.         {
    58.             Launch();
    59.         }
    60.         else
    61.         {
    62.             if (!hasDetected)
    63.             {
    64.                 GetComponent<CapsuleCollider>().radius = 10;
    65.                 GetComponent<CapsuleCollider>().height = 300;
    66.                 //RotateToMousePosition();
    67.                 if (selected)
    68.                 {
    69.                     CalculatePath();
    70.                 }
    71.                 if (linePoints.Count > 0)
    72.                 {
    73.                     FollowPath();
    74.                     //InterpolatePath();
    75.                 }
    76.             }
    77.             if (hasDetected)
    78.             {
    79.                 RotateToMissile();
    80.             }
    81.         }
    82.         if (selected)
    83.         {
    84.             this.gameObject.transform.GetChild(0).gameObject.SetActive(true);
    85.             //Debug.Log(this.gameObject.transform.GetChild(0).gameObject);
    86.         }
    87.         else
    88.         {
    89.             deselected = true;
    90.             this.gameObject.transform.GetChild(0).gameObject.SetActive(false);
    91.         }
    92.         //Debug.Log(current);
    93.         //Debug.Log(linePoints.Count);
    94.         if (deselected && selected)
    95.         {
    96.             linePoints.Clear();
    97.             current = 1;
    98.             deselected = false;
    99.         }
    100.     }
    101.  
    102.     void Launch()
    103.     {
    104.         dist = 0.2 - transform.position.y;
    105.         if (dist > 0.01)
    106.         {
    107.             transform.position = Vector3.Slerp(transform.position, new Vector3(transform.position.x, 0.2f, transform.position.z), moveSpeed * Time.deltaTime);
    108.         }
    109.         else
    110.         {
    111.             transform.localScale = new Vector3(0.01f, 0.004f, 0.02f);
    112.             launched = true;
    113.         }
    114.  
    115.     }
    116.  
    117.     private void OnTriggerEnter(Collider other)
    118.     {
    119.         if (other.tag == "LaunchedMissile")
    120.         {
    121.             hasDetected = true;
    122.             missileTarget = other.gameObject;
    123.         }
    124.     }
    125.  
    126.  
    127.  
    128.     void CalculatePath()
    129.     {
    130.         // Create objects in between since the mouse moves faster than the update!!
    131.         // Linear interpolation, beizel curve
    132.         Vector3 mousePos = Input.mousePosition;
    133.         //mousePos.z = thisCamera.nearClipPlane;
    134.         //Vector3 mouseWorld = thisCamera.ScreenToWorldPoint(mousePos);
    135.         int layerMask = 1 << 8; //We only want to raycast with the hitbox
    136.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    137.         RaycastHit hit;
    138.         if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask))
    139.         {
    140.             Vector3 mouseWorld = new Vector3(hit.point.x, hit.point.y, hit.point.z);
    141.             float dist = Vector3.Distance(lastPos, mouseWorld);
    142.             if (dist <= threshold)
    143.                 return;
    144.  
    145.             lastPos = mouseWorld;
    146.             if (linePoints == null)
    147.                 linePoints = new List<Vector3>();
    148.             linePoints.Add(mouseWorld);
    149.  
    150.             UpdateLine();
    151.         }
    152.  
    153.  
    154.     }
    155.  
    156.     void UpdateLine()
    157.     {
    158.         lineRenderer.SetWidth(startWidth, endWidth);
    159.         lineRenderer.SetVertexCount(linePoints.Count);
    160.  
    161.         for (int i = lineCount; i < linePoints.Count; i++)
    162.         {
    163.             lineRenderer.SetPosition(i, linePoints[i]);
    164.         }
    165.         lineCount = linePoints.Count;
    166.     }
    167.  
    168.     void InterpolatePath()
    169.     {
    170.         for (int i = 1; i < linePoints.Count; i++)
    171.         {
    172.             //ry
    173.             //{
    174.             dist = Vector3.Distance(linePoints[i], linePoints[i + 1]);
    175.             Debug.Log(dist);
    176.             if (dist > 0.2)
    177.             {
    178.                 for (int j = 0; i < numPoints; i++)
    179.                 {
    180.                     float t = j / numPoints;
    181.                     linePoints.Insert(i + 1, CalculateLinearBezierpoint(t, linePoints[i], linePoints[i + j]));
    182.                 }
    183.             }
    184.             //} catch
    185.             //{
    186.  
    187.             //}
    188.         }
    189.     }
    190.  
    191.     private void DrawLinearCurve()
    192.     {
    193.         for (int i = 0; i < numPoints; i++)
    194.         {
    195.             float t = i / numPoints;
    196.         }
    197.     }
    198.  
    199.     private Vector3 CalculateLinearBezierpoint(float t, Vector3 p0, Vector3 p1)
    200.     {
    201.         return p0 + t * (p1 - p0);
    202.     }
    203.  
    204.     void FollowPath()
    205.     {
    206.         //transform.position = Vector3.Slerp(transform.position, testPos.transform.position, speed * Time.deltaTime);
    207.         try
    208.         {
    209.             float dist = Vector3.Distance(linePoints[current], transform.position);
    210.             transform.position = Vector3.Slerp(transform.position, linePoints[current], speed * Time.deltaTime);
    211.  
    212.             var rotation = Quaternion.LookRotation(linePoints[current] - transform.position);
    213.             transform.rotation = Quaternion.Slerp(transform.rotation, rotation, rotationSpeed * Time.deltaTime);
    214.             if (dist <= 0.01f)
    215.             {
    216.                 //Debug.Log(linePoints.Count);
    217.                 linePoints.Remove(linePoints[current - 1]);
    218.                 current++;
    219.             }
    220.  
    221.  
    222.             if (linePoints.Count > 50 && (linePoints.Count - current) < 10)
    223.             {
    224.                 Destroy(gameObject);
    225.                 Debug.Log("Boom!");
    226.             }
    227.         }
    228.         catch
    229.         {
    230.             current++;
    231.         }
    232.     }
    233.  
    234.     private void SelfDestruct()
    235.     {
    236.         Destroy(gameObject);
    237.     }
    238.  
    239.     private void OnDrawGizmos()
    240.     {
    241.         if (drawGizmos)
    242.         {
    243.             try
    244.             {
    245.                 for (int i = 0; i < linePoints.Count; i++)
    246.                 {
    247.                     //Debug.Log(i);
    248.                     Gizmos.DrawWireSphere(linePoints[i], (float)0.005);
    249.  
    250.                 }
    251.             }
    252.             catch
    253.             {
    254.  
    255.             }
    256.         }
    257.     }
    258.  
    259.  
    260.     void RotateToMousePosition()
    261.     {
    262.         ground = new Plane(Vector3.up, transform.position);
    263.  
    264.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    265.  
    266.         //*distance only needs an initial value to work
    267.         float dist = 0f;
    268.  
    269.         if (ground.Raycast(ray, out dist))
    270.         {
    271.             //create a vector position at latest raycast hit point
    272.             Vector3 mousePoint = new Vector3(ray.GetPoint(dist).x,
    273.                 transform.position.y, ray.GetPoint(dist).z);
    274.  
    275.             //*useful for showing where ray comes from in testing ONLY
    276.             Debug.DrawLine(ray.origin, mousePoint);
    277.  
    278.             //rotate object toward vector position
    279.             Quaternion targetRotation = Quaternion.LookRotation(mousePoint - transform.position);
    280.             transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
    281.  
    282.             //transform.rotation = new Quaternion(90, transform.rotation.z, transform.rotation.z, transform.rotation.w);
    283.             transform.position = Vector3.Slerp(transform.position, new Vector3(mousePoint.x, 0.2f, mousePoint.z), moveSpeed * Time.deltaTime);
    284.         }
    285.     }
    286.  
    287.     void RotateToMissile()
    288.     {
    289.         dist = Vector3.Distance(missileTarget.transform.position, transform.position);
    290.         //rotate object toward vector position
    291.         Quaternion targetRotation = Quaternion.LookRotation(missileTarget.transform.position - transform.position);
    292.         transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * 5 * Time.deltaTime);
    293.  
    294.         //transform.rotation = new Quaternion(90, transform.rotation.z, transform.rotation.z, transform.rotation.w);
    295.         transform.position = Vector3.Slerp(transform.position, new Vector3(missileTarget.transform.position.x, missileTarget.transform.position.y, missileTarget.transform.position.z), moveSpeed * 5 * Time.deltaTime);
    296.         if (dist < 0.02)
    297.         {
    298.             spawned = false;
    299.             Instantiate(explosion, transform.position, Quaternion.identity);
    300.             Destroy(missileTarget);
    301.             Destroy(gameObject);
    302.         }
    303.  
    304.     }
    305. }
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Don't you want to reset current to 0 instead of 1?
     
  3. Bogaland

    Bogaland

    Joined:
    Feb 19, 2017
    Posts:
    32
    I tried that before which is why I later had current = 1 (since I access current -1 later I figured that was the problem or something). But nope, that did not solve it either.

    UPDATE: When I reduced the speed of my missile, which I wanted in the first place, it seems to work most of the time. However, sometimes it just stops and lags as before. So I guess the main solution works most of the time. Any idea why it sometimes won't work?
     
    Last edited: Jul 11, 2018