1

This is Player script:

public class player
{      public projectile pro;
       pro = GetComponent<projectile>();
    void Update()
    {
        GameObject go = GameObject.Find("enemy");
        Transform playerTransform = go.transform;
        Vector3 posi = playerTransform.position;
        pro.Target = posi; // getting error here
        Instantiate(bulletprefab, position, Quaternion.identity);   
    }
}

This is Projectile Script: Requirement: Get updated position of Target in Projectile but since projectileMotion() method is being called from Start() and I want the Target current's position at point when Instantiate is called in Player so as to calculate the Target_Distance appropriately in Projectile class, But looks like the assignment from a Vector3 to _pro.Target which is a vector3 only is not allowed. Ho can I fix this?

 public class projectile : MonoBehaviour 
{
    public Vector3 Target;
    public GameObject bulletprefab;
        public float firingAngle = 45.0f;
        public float gravity = 9.8f;
       public Transform Projectile;      
        private Transform myTransform;

        void Awake()
        {
            myTransform = transform;  

        }

        void Start()
    {    myTransform.LookAt(Target);
        StartCoroutine (ProjectileMotion ());

        }



        IEnumerator ProjectileMotion()
        {

            yield return new WaitForSeconds(0.25f);


            Projectile.position = myTransform.position + new Vector3(0, 0.0f, 0);

            // Calculating distance to target
        float target_Distance = Vector3.Distance(Projectile.position, Target );
        Debug.Log ("realUPDATEDOR not" + Target);

            float projectile_Velocity = target_Distance / (Mathf.Sin(2 * firingAngle* Mathf.Deg2Rad) / gravity);

            // X  Y componenent of the velocity
            float Vx = Mathf.Sqrt(projectile_Velocity) * Mathf.Cos(firingAngle * Mathf.Deg2Rad);
            float Vy = Mathf.Sqrt(projectile_Velocity) * Mathf.Sin(firingAngle * Mathf.Deg2Rad);

            // flight time since depends on horizontal component of velocity
            float flightDuration = target_Distance / Vx;

            // projectile rotated at target
            Projectile.rotation = Quaternion.LookRotation(Target - Projectile.position);

            float elapse_time = 0;

            while (elapse_time < flightDuration)               //looping and incrementing elapsed time
            {
                Projectile.Translate(0, (Vy - (gravity * elapse_time)) * Time.deltaTime, Vx * Time.deltaTime);

                elapse_time += Time.deltaTime;

                yield return null;
            }
        }  
    }

Target is present is Projectile class and is Vector3 only, how to fix this error?

systemdebt
  • 4,589
  • 10
  • 55
  • 116

1 Answers1

0

the code you get a reference to the "projectile" script wont work.

change :

public projectile pro;    
pro = GetComponent<projectile>();

to something like this:

public projectile pro;
void Start()
{
pro = GetComponent<projectile>();
}

Also as a suggestion you might wanna reduce the GameObject.Find uses inside the update function cause of its high cost.

Nergon
  • 443
  • 2
  • 14