Search Unity

Hi can you review this code

Discussion in 'Scripting' started by Tangoxx, Mar 31, 2015.

?

good/bad

  1. good

    0 vote(s)
    0.0%
  2. bad

    0 vote(s)
    0.0%
  1. Tangoxx

    Tangoxx

    Joined:
    Mar 31, 2015
    Posts:
    2
    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<Rigidbody>().velocity = transform.forward * speed;


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

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

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

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
  3. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    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.
     
    Last edited: Mar 31, 2015
    BenZed likes this.