Search Unity

[Redacted] Make a script accessible like Mathf?

Discussion in 'Scripting' started by dragonice501, Apr 30, 2017.

  1. dragonice501

    dragonice501

    Joined:
    Dec 2, 2015
    Posts:
    146
    I would like to access this scripts ShootPrediction function similar to how you would any Mathf function but am not completely sure how to do so.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SS_Shoot_Predict : MonoBehaviour {
    5.  
    6.     Vector3 ShotPrediction (Transform position, Transform target, Rigidbody bullet)
    7.     {
    8.         Vector3 relativeTargetposition = position.position + (target.position - transform.position);
    9.         Vector3 relativeTargetVelocity = target.GetComponent<Rigidbody> ().velocity;
    10.         float speed = bullet.GetComponent<SS_Bullet> ().speed;
    11.  
    12.         float t = TimeToImpact (relativeTargetposition, relativeTargetVelocity, speed);
    13.         return relativeTargetposition + relativeTargetVelocity * t;
    14.     }
    15.  
    16.     float TimeToImpact (Vector3 position, Vector3 relativeTargetVelocity, float bulletSpeed)
    17.     {
    18.         float a = bulletSpeed * bulletSpeed - (Vector3.SqrMagnitude (relativeTargetVelocity));
    19.         float b = Vector3.Dot (position, relativeTargetVelocity);
    20.         float c = Vector3.SqrMagnitude (position);
    21.  
    22.         float d = b * b + a * c;
    23.         float t = 0;
    24.  
    25.         if (d >= 0)
    26.         {
    27.             t = (b + Mathf.Sqrt (d)) / a;
    28.             if (t < 0)
    29.                 t = 0;
    30.         }
    31.  
    32.         return t;
    33.     }
    34. }
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Make it a static class. Make all of its members static.

    This approach is ideal for a stateless helper class, like Mathf.
     
    LovesSolvingProblems likes this.