summaryrefslogtreecommitdiff
path: root/Assets/Art/Vfx/StylizedProjectilePack1/WebDemo/scripts/Projectile.cs
blob: 69c939c6840b623da1ea5b01ad41b89dab67d7ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace StylizedProjectile
{


public class Projectile : MonoBehaviour
{
    private float disappearAfterTime = 3;
    private float disappearTimer = 0;

    private bool isActive = false;

  //  private MeshRenderer meshRenderer;

    private Vector3 direction;
    private Vector3 startPosition;

    private float movementSpeed = 1;

    private Vector3 targetPosition;

    private float distanceToTarget;
    private float movementValue;

	// Use this for initialization
	void Start ()
	{
        // We cache this for performance reasons, GetComponent is slow to do realtime
        // meshRenderer = GetComponent<MeshRenderer>(); 
    //    meshRenderer = GetComponent<MeshRenderer>();
	}
	
	// Update is called once per frame
	void Update () {
	    if (isActive) // Only update stuff if we're alive
	    {
	        disappearTimer += Time.deltaTime; // Increase disappear timer
	        if (disappearTimer > disappearAfterTime) // If we're alive too long, get rekt
	        {
	            disappearTimer = 0; // Reset timer
	            isActive = false; // Is not active anymore
	    //        meshRenderer.enabled = false; // Disable meshrender so it's invisible
	        }

	        
            // 1/distanceToTarget is the calculation to move 1 unit per second, movementspeed defines how many units per second you want to move
	        movementValue += (1/distanceToTarget*movementSpeed) * Time.deltaTime;
	        if (movementValue > 1)
	        {
	            movementValue = 1;
                Explode();
	        }
	        Move();
	        
	    }
	}

    void Move()
    {
        // lerp goes from 0 to 1, 0 is startPosition, 1 is the targets position;
        transform.position = Vector3.Lerp(startPosition, targetPosition, movementValue);
    }

    void MoveWithoutTargetHit()
    {
        transform.position += direction.normalized * movementSpeed;
    }

    public void Fire(Vector3 target, Vector3 spawnPosition, Vector3 Direction, float speed)
    {
        if (isActive) // If we're active, just return so we don't execute any code
            return;
        
        isActive = true; // Set active
        disappearTimer = 0; // Reset timer just in case it's not reset
        transform.position = spawnPosition; // set spawn position
       // meshRenderer.enabled = true; // Enable meshrender so it's visible
        movementSpeed = speed; // Units per second
        direction = Direction.normalized; // Normalize the direction
        targetPosition = target; // Set target transform - Can be null for continous movement without target
        distanceToTarget = Vector3.Distance(targetPosition, transform.position);
        startPosition = spawnPosition;
        movementValue = 0;
    }

    // It gets here after it hits something
    void Explode() 
    {
        disappearTimer = 0; // Reset timer
        isActive = false; // Is not active anymore
     //   meshRenderer.enabled = false; // Disable meshrender so it's invisible
    }

    public bool GetIsActive()
    {
        return isActive;
    }
}
}