Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Explain this script step by step?

Discussion in 'Scripting' started by DoraemonCrayon, Nov 25, 2019.

  1. DoraemonCrayon

    DoraemonCrayon

    Joined:
    Mar 10, 2019
    Posts:
    27
    It´s a pursue script learned in college and I want to understand every part of this script;




    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Pursue : MonoBehaviour
    {
    public float alpha = 0.01f;

    public Move target;

    Vector2 targetPosition;

    Vector3 direction;
    Vector3 velocity;

    public float speed = 5;

    // =========================
    Vector3 desired_velocity;
    Vector3 steering_velocity;
    public float mass = 10;

    private void Update()
    {
    targetPosition = GetFuturePosition();

    direction = ((Vector3)targetPosition - transform.position).normalized;
    desired_velocity = direction * speed * Time.deltaTime;

    steering_velocity = desired_velocity - velocity;
    steering_velocity = steering_velocity / mass;

    velocity = velocity + steering_velocity;

    transform.position += velocity;

    Rotate();
    }

    void Rotate()
    {
    float angle = Mathf.Atan2(velocity.y, velocity.x) * Mathf.Rad2Deg;
    transform.eulerAngles = new Vector3(0, 0, angle);
    }

    Vector2 GetFuturePosition()
    {
    if (target.velocity.magnitude > 0.5f)
    {
    float distance = Vector2.Distance(transform.position, target.transform.position);
    float T = distance / velocity.magnitude * alpha;

    return (Vector2)target.transform.position + (target.velocity * T);
    }
    else
    return target.transform.position;
    }

    private void OnDrawGizmos()
    {
    Gizmos.color = Color.red;
    Gizmos.DrawWireSphere(targetPosition, 0.5f);
    }
    }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    First up, please use code tags when posting code. See the first post in the forum.

    Second, this script is VERY basic. If you do just one or two Unity scripting tutorials, you will have enough information to start to understand it.

    Third, once you've gotten that far, just use google to look up any functions that you are unsure about. If it is an unusual name like OnDrawGizmos() the answer will be clear. If it is a less-common name like "Update()" then be sure to include something like "unity scripting update" when googling.
     
    Antypodish likes this.
  3. DoraemonCrayon

    DoraemonCrayon

    Joined:
    Mar 10, 2019
    Posts:
    27

    Where do I access Unity Scripting Tutorial?
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
  5. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,926
    I've had nothing but trouble with mixing Vector2's with Vector3's (targetPosition and the function GetFuturePosition). The idea seems fine: to pick a spot on the map, you only need an x and y. The z is however tall the map is at that point. The game doesn't have submarines in it, after all. The problem is tat Unity likes y to be up, so a map
    position is x and Z. Vector2 p = transform.position doesn't know that. It copies x and y, slicing off z, giving wrong answers in a confusing way.

    atan2 is also a clunky way to compute the direction of a line. LookDirection(velocity) is more obvious and common.

    My guess is this is a "common-looking" script. Maybe it was used last semester, or/and the parts came from a site lots of other students can see, and the textbook has examples like this. If it was changed, students would ask why it looks different from other scripts -- what does LookDirection do? Is it like atan2 from the book? And that's a real thing -- a "worse looking" way that people have seen is better than a better-looking way they've never seen.

    I'm not saying the script is bad. I'm saying not to take it too seriously. For example "velocity = velocity + steering_velocity; transform.position += velocity;" Is there a reason for not using velocity+= ? Or do we try to avoid += but we had to use it for transform.position+= ? No and no. The two lines are different for no reason.