Search Unity

Instanced projectile not moving toward target

Discussion in 'Scripting' started by Holocene, Feb 24, 2017.

  1. Holocene

    Holocene

    Joined:
    Feb 21, 2009
    Posts:
    347
    Hi all,

    I'm successfully instantiating my projectile, but not able to affect its rigibody with GetComponent.
    Any ideas?




    Code (CSharp):
    1. // Converted from UnityScript to C# at http://www.M2H.nl/files/js_to_c.php - by Mike Hergaarden
    2. // Do test the code! You usually need to change a few small bits.
    3.  
    4. using UnityEngine;
    5. using System.Collections;
    6.  
    7. public class ShooterforC : MonoBehaviour {
    8. public Transform character;
    9.  
    10.  
    11.  
    12.   public GameObject projectile;
    13.   public float fireRate = 0.5F;
    14.   private float nextFire = 0.0F;
    15.   public Camera main;
    16.     private Transform clone;
    17.    RaycastHit hit;
    18.   void Update()
    19.   {
    20.       if (Input.GetMouseButton(0) && Time.time > nextFire) {
    21.      //if (Input.GetButton("Fire1") && Time.time > nextFire)
    22.      {
    23.        
    24.          RaycastHit hit;
    25.             float distance = 100;
    26.             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    27.             if(Physics.Raycast(ray,out hit)){
    28.  
    29.        
    30.          Debug.DrawLine (character.position, hit.point);
    31.  
    32.        
    33.          nextFire = Time.time + fireRate;
    34.          GameObject clone = Instantiate(projectile, transform.position, transform.rotation) as GameObject;
    35.        
    36.          nextFire = Time.time + fireRate;
    37.  
    38.           projectile.transform.LookAt(hit.point);
    39.  
    40.       projectile.GetComponent<Rigidbody>().velocity = projectile.transform.forward * 15;
    41.  
    42.        
    43.      }
    44.   }
    45. }
    46. }}
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you're trying to update the prefab, not the instance, try using "clone" in lines 38 40