Search Unity

smoothfollow attached to enemy not following player

Discussion in 'Scripting' started by airesdav, Mar 30, 2013.

  1. airesdav

    airesdav

    Joined:
    Nov 13, 2012
    Posts:
    128
    Hi guys i was wondering if you could help with a dilema i am having. I have a first person controler acting as a Player and i have an emeny that spawns multiple enemies so far so good, i have used a smooth follow script that someone had written from here in the community in C#, added the below code to the enemy and selected the first person controller as my target but fro some reason when these cubes spawn they go right past me but i want them to follow me can someone please look at both my spawning code or the smooth as i cannot understand why it would do this:;)

    smoothfollow

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5.  
    6.  
    7.  
    8.  
    9. public class SmoothFollow : MonoBehaviour
    10.  
    11.  
    12. {
    13.  
    14.  
    15.     #region Consts
    16.  
    17.  
    18.     private const float SMOOTH_TIME = 5.3f;
    19.  
    20.  
    21.     #endregion
    22.  
    23.  
    24.  
    25.  
    26.     #region Public Properties
    27.  
    28.  
    29.     public bool LockX;
    30.  
    31.  
    32.     public bool LockY;
    33.  
    34.  
    35.    public bool LockZ;
    36.  
    37.  
    38.     public bool useSmoothing;
    39.  
    40.  
    41.     public Transform target;
    42.  
    43.  
    44.     #endregion
    45.  
    46.  
    47.  
    48.  
    49.  
    50.     #region Private Properties
    51.  
    52.  
    53.     private Transform thisTransform;
    54.  
    55.  
    56.     private Vector3 velocity;
    57.  
    58.  
    59.    #endregion
    60.  
    61.  
    62.  
    63.  
    64.  
    65.     private void Awake()
    66.  
    67.  
    68.     {
    69.  
    70.  
    71.         thisTransform = transform;
    72.  
    73.  
    74.  
    75.  
    76.  
    77.         velocity = new Vector3(5.5f, 5.5f, 5.5f);
    78.  
    79.  
    80.     }
    81.  
    82.  
    83.  
    84.  
    85.  
    86. // ReSharper disable UnusedMember.Local
    87.  
    88.  
    89.     private void LateUpdate()
    90.  
    91.  
    92. // ReSharper restore UnusedMember.Local
    93.  
    94.  
    95.     {
    96.  
    97.  
    98.         var newPos = Vector3.zero;
    99.  
    100.  
    101.  
    102.  
    103.  
    104.         if (useSmoothing)
    105.  
    106.  
    107.         {
    108.  
    109.  
    110.             newPos.x = Mathf.SmoothDamp(thisTransform.position.x, target.position.x, ref velocity.x, SMOOTH_TIME);
    111.  
    112.  
    113.             newPos.y = Mathf.SmoothDamp(thisTransform.position.y, target.position.y, ref velocity.y, SMOOTH_TIME);
    114.  
    115.  
    116.             newPos.z = Mathf.SmoothDamp(thisTransform.position.z, target.position.z, ref velocity.z, SMOOTH_TIME);
    117.  
    118.  
    119.         }
    120.  
    121.  
    122.         else
    123.  
    124.  
    125.         {
    126.  
    127.  
    128.             newPos.x = target.position.x;
    129.  
    130.  
    131.             newPos.y = target.position.y;
    132.  
    133.  
    134.             newPos.z = target.position.z;
    135.  
    136.  
    137.         }
    138.  
    139.  
    140.  
    141.  
    142.  
    143.         #region Locks
    144.  
    145.  
    146.         if (LockX)
    147.  
    148.  
    149.         {
    150.  
    151.  
    152.             newPos.x = thisTransform.position.x;
    153.  
    154.  
    155.         }
    156.  
    157.  
    158.  
    159.  
    160.  
    161.         if (LockY)
    162.  
    163.  
    164.         {
    165.  
    166.  
    167.             newPos.y = thisTransform.position.y;
    168.  
    169.  
    170.         }
    171.  
    172.  
    173.  
    174.  
    175.  
    176.         if (LockZ)
    177.  
    178.  
    179.         {
    180.  
    181.  
    182.             newPos.z = thisTransform.position.z;
    183.  
    184.  
    185.         }
    186.  
    187.  
    188.         #endregion
    189.  
    190.  
    191.  
    192.  
    193.  
    194.         transform.position = Vector3.Slerp(transform.position, newPos, Time.time);
    195.  
    196.  
    197.     }
    198.  
    199.  
    200. }
    201.  
    202.  
    203.  
    204.  
    spawn code:


    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6.     public class zombiespawn : MonoBehaviour
    7.     {
    8.     // Spawn location
    9.      public Vector3 spawnLocation = Vector3.zero;
    10.     // Spawn radius (Gives a bit more randomness factor to the spawn location)
    11.     public float spawnRadius = 4.0f;
    12.     // Spawn timer (seconds)
    13.     public float spawnTimer = 3.0f;
    14.     private float spawnTimeRemaining = 5.0f;
    15.    
    16.    
    17.    
    18.    
    19.    
    20.    
    21.     // The zombie to spawn
    22.     public GameObject zombiePrefab = null;
    23.    
    24.    
    25.  
    26.     void Awake()
    27.     {
    28.     spawnTimeRemaining = spawnTimer;
    29.     }
    30.    
    31.    
    32.    
    33.  
    34.  
    35.  
    36.    
    37.  
    38.     void FixedUpdate()
    39.     {
    40.     spawnTimeRemaining -= Time.deltaTime;
    41.  
    42.     if (spawnTimeRemaining < 0.0f)
    43.     {
    44.       Vector2 circlePosition = Random.insideUnitCircle * spawnRadius;
    45.       GameObject.Instantiate(zombiePrefab, spawnLocation + new Vector3(circlePosition.x, 0.0f, circlePosition.y), Quaternion.identity);
    46.  
    47.       spawnTimeRemaining = spawnTimer;
    48.     }
    49.   }
    50. }
    51.    
    52.  
    53.  
    54.