Rope Projectile Dissection

Here is a code for the Rope projectile

public class RopeProjectile : Projectile

{

    public int           PointsToGiveToPlayer;

    public float       MaxDistanceToLive;

    public int             Damage;

    private Transform _projectileSpawner;

    private bool       _returnToOrigin     = false;

    private bool       _returned         = false;

    public void Start()

    {

        _projectileSpawner = Weapon.ProjectileFireLocation;

    }

    public void Update ()

    {

        if (_returned)

        {

            DestroyProjectile();

        }

        if ( (MaxDistanceToLive -= Time.deltaTime * Speed) <= 0)

        {

            ReturnToOriginProjectile(true);

        }

        if (! _returned && _returnToOrigin && _projectileSpawner)

        {

            // translate projectile toward the destination transform

            transform.position = Vector3.MoveTowards (transform.position, _projectileSpawner.transform.position, Time.deltaTime * Speed * 2.0f);

            var distanceSquared = (_projectileSpawner.transform.position – transform.position).sqrMagnitude;

            if (distanceSquared > 0.01f * 0.01f)

                return;

            else

                _returned = true;

        }

        transform.Translate ( Direction * ((Mathf.Abs(InitialVelocity.x) + Speed) * Time.deltaTime), Space.World );

    }

    private void DestroyProjectile()

    {

        Destroy (gameObject);

    }

    private void ReturnToOriginProjectile(bool returnToOrigin)

    {

        _returnToOrigin = returnToOrigin;

    }

}

We are working on making the Rope Projectile trail at runtime.

So basically we will add up gameobjects with rope trail sprites on it. But not sure if this can handle the Vector Lerp when Rope gets back. We are open to suggestions.

Here is our Projectile Abstract Class

public abstract class Projectile : MonoBehaviour 

{

    public LayerMask     CollisionMask;

    public GameObject   Owner {getprivate set;}

    public Vector2         Direction {getprivate set;}

    public Vector2         InitialVelocity {getprivate set;}

    public float         Speed;

    public void Initialize (GameObject owner, Vector2 direction, Vector2 initialVelocity)

    {

        transform.right = direction;

        Owner = owner;

        Direction = direction;

        InitialVelocity = initialVelocity;

        OnInitialized();

    }

    protected virtual void OnInitialized ()

    {

    }

    public virtual void OnTriggerEnter2D (Collider2D other)

    {

        if ( (CollisionMask.value & (1 << other.gameObject.layer) ) == 0 )

        {

            OnNotCollideWith (other);

            return;

        }

        var isOwner = other.gameObject == Owner;

        if (isOwner)

        {

            OnCollideOwner();

            return;

        }

        var takeDamage = (ITakeDamage) other.GetComponent(typeof (ITakeDamage));

        if ( takeDamage != null )

        {

            OnCollideTakeDamage (other, takeDamage);

            return;

        }

        OnCollideOther(other);

    }

}

Thanks for reading.

if you want to check out some of our projects please subscribe to our YouTube channel:

You can check up our work in progress games here:

If you are looking for solutions to game development issues see our blogs here:

If you like our projects please sign up for our newsletter at the bottom of this Page.

Leave a Reply