Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How can i do for my Tower follow the Player??

Discussion in 'Scripting' started by Ericmatheus, Nov 21, 2014.

  1. Ericmatheus

    Ericmatheus

    Joined:
    Aug 11, 2014
    Posts:
    68
    I have a tower that i made on Blender
    What i need is to the "tower's cannon" (in front of the window,see the picture)follow the player,the cannon rotate depend on the player positon,May i Use vector3.Distance something like this??
    And i'm gonna make a projectile,the tower gonna shoot,but i need that the projectile try to follow the player too
    Thanks in advance
     

    Attached Files:

    • towe.jpg
      towe.jpg
      File size:
      476.6 KB
      Views:
      639
  2. SeriousBusinessFace

    SeriousBusinessFace

    Joined:
    May 10, 2014
    Posts:
    127
    A suggestion in code:

    Code (csharp):
    1.  
    2. Vector3 forward = (transform.position - _Target.transform.position).normalized;
    3. transform.rotation = Quaternion.LookRotation(forward, Vector3.up);
    4.  
    Where "_Target" is a variable containing the player. For the projectile, I'd suggest adding the following:
    Code (csharp):
    1.  
    2. (Code as above)
    3. transform.position += forward * (speed * Time.deltaTime);
    4.  
    Or Time.fixedDeltaTime if your code is in the FixedUpdate() function instead of the Update() function. If you're using a RigidBody, the two lines become:
    Code (csharp):
    1.  
    2. rigidbody.rotation = Quaternion.LookRotation(forward, Vector3.up);
    3. rigidbody.MovePosition(rigidbody.position + forward * (speed * Time.deltaTime);
    4.  
    Hopefully that helps. :)
     
    Ericmatheus likes this.
  3. Ericmatheus

    Ericmatheus

    Joined:
    Aug 11, 2014
    Posts:
    68
    i'll check it Thanks
     
  4. MrReclusive

    MrReclusive

    Joined:
    Nov 16, 2014
    Posts:
    14
    i was actually just doing the same thing.
    here's the code i ended up using, it has all the code for audio and spawning of the bullet as well.

    Code (CSharp):
    1.  
    2. public class GuardTower : MonoBehaviour {
    3.     public Rigidbody bullet;
    4.     private Vector3 spawnSpot;
    5.     private Quaternion spawnrot;
    6.     public AudioClip riffleshoot;
    7.     float delay = 5.0f;
    8.     GameObject FindClosestEnemy() {
    9.                 GameObject[] gos;
    10.                 gos = GameObject.FindGameObjectsWithTag ("Enemy");
    11.                 GameObject closest = null;
    12.                 float distance = Mathf.Infinity;
    13.                 Vector3 position = transform.position;
    14.                 foreach (GameObject go in gos) {
    15.                         Vector3 diff = go.transform.position - position;
    16.                         float curDistance = diff.sqrMagnitude;
    17.                         if (curDistance < distance) {
    18.                                 closest = go;
    19.                                 distance = curDistance;
    20.                         }
    21.                 }
    22.                 return closest;
    23.         }
    24.     float distance;
    25.  
    26.  
    27.     // Use this for initialization
    28.     void Start () {
    29.         spawnSpot = this.transform.position;
    30.         spawnrot = this.transform.rotation;
    31.         delay = 0;
    32.    
    33.     }
    34.    
    35.     // Update is called once per frame
    36.     void Update () {
    37.         this.transform.LookAt (FindClosestEnemy().transform.position);
    38.         spawnSpot = this.transform.position;
    39.         spawnrot = this.transform.rotation;
    40.         delay -= 1 * Time.deltaTime;
    41.         distance = Vector3.Distance (transform.position, FindClosestEnemy ().transform.position);
    42.         if (distance <= 10.0f)
    43.         {//Debug.Log ("oh S*** oh S*** OH S***");
    44.             if (delay <= 0)
    45.             {
    46.  
    47.                 GameObject cubeSpawn = (GameObject)Instantiate (bullet, spawnSpot, spawnrot );
    48.                 audio.clip = riffleshoot;
    49.                 audio.Play ();
    50.                 delay = 5.0f;
    51.             }
    52.         }
    53.    
    54.     }
    55. }
     
  5. MrReclusive

    MrReclusive

    Joined:
    Nov 16, 2014
    Posts:
    14
    my projectiles do not follow player thought, mine is a manned tower with a guy shooting a riffle, id assume you could add the findclosestenemy to your projectile to make that work. also, if anyone knows, is their a way to limit the rotation? obviously you dont want your tower aiming straight down.
     
    Ericmatheus likes this.
  6. Ericmatheus

    Ericmatheus

    Joined:
    Aug 11, 2014
    Posts:
    68
    thank you
    Aprreciated