Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

aiming at a target

Discussion in 'Scripting' started by trinitysj, Mar 3, 2010.

  1. trinitysj

    trinitysj

    Joined:
    Jan 15, 2010
    Posts:
    54
    does this look right.

    Code (csharp):
    1.  
    2. if (Input.GetKeyDown ("1"))
    3.     {
    4.         // This will make the character turn to the enemy
    5.         look_at = Quaternion.LookRotation(enemies_list[aiming_at].GetComponent(Transform).position - transform.position);
    6.         look_at.x = 0;
    7.         look_at.z = 0;
    8.         transform.rotation = Quaternion.Lerp(transform.rotation, look_at, track_speed);
    9.        
    10.         //instantiate the projectile
    11.         var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation);
    12.        
    13.         //move the projectile
    14.         //instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0,0,speed));
    15.         transform.LookAt(enemies_list[aiming_at].GetComponent(Transform));
    16.         transform.Translate(0,10 * Time.deltaTime, Time.deltaTime);
    17.         instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0,0,speed));
    18.    
    19.         Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);
    20.         Destroy(instantiatedProjectile.gameObject, 5);
    21.        
    22.         print (aiming_at);
    23.        
    24.      }
    25.  
    each time I press the 1 key my toon moves up in the air...
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    I'm not really sure why you create the instantiatedProjectile and then immediately destroy it. Is it supposed to exist for only one frame?