Hi can you review this code

using UnityEngine;
using System.Collections;
//script only works if you have a tag named Player
//for best effect use a prefab
public class MissileScript : MonoBehaviour
{

public Transform target;
public float speed = 3f;
private bool ifTarget = false;

public void SetTarget(Transform helTrag)
{
target = helTrag;
ifTarget = true;
}
// Use this for initialization
void Start ()
{
SetTarget (target); //temp. call set target when intsintingings

}

void Update ()
{
if(ifTarget)
{

//roates the projectile this make it able for you to dodge the following projectile
Vector3 toTarget = target.position - transform.position;
Quaternion rotation = Quaternion.LookRotation(toTarget);

transform.rotation = Quaternion.RotateTowards(transform.rotation, rotation, Time.deltaTime*250f);
//lucnh the projectile fowards
GetComponent().velocity = transform.forward * speed;

//transform.rotation = new Quaternion(Mathf.LerpAngle(transform.rotation.x,
//transform.LookAt(toTarget);

//Vector3 toTarget = target.position - transform.position;
//GetComponent().velocity = toTarget.normalized * speed;

//transform.forward = toTarget.normalized;
}
}
}
this is my first post, dont be rude!

Please use code tags.

Can you please use the CODE tags it makes if a lot easier to read code on fourms.

Also your code is fine as long as it does its intended job, the quality of your code will only go up with experience.

A major problem I see is your use of GetComponent in the Update method, you should use the GetComponent in your start method and save it to a private field on the class. This way you are only performing the Costly GetComponent method once, and just referencing its result in your update.

1 Like