コード例 #1
0
    public IEnumerator FireObject()
    {
        canAttack = false;

        // Create delay btw firing
        yield return(new WaitForSeconds(1.0f)); //we don't want it to be next frame

        canAttack = true;

        // Create BuckyBall
        GameObject myProjectile;

        myProjectile = Instantiate(projectile, transform.position, Quaternion.identity);

        RangedMonsterBombManager ranged = myProjectile.GetComponent <RangedMonsterBombManager>();

        ranged.mother = this.gameObject;

        //Debug.Log("Ball made");

        //Move Projectile to the position of Monster + add some offset
        myProjectile.transform.position = rangedMonsterTransform.position + new Vector3(-0.1f, 0.5f, -0.5f);

        //Calculate distance btw target and the ball
        float targetDistance = Vector3.Distance(myProjectile.transform.position, targetEntity.transform.position);

        // Calculate the velocity needed to throw the object to the target at specified angle
        // sin2a = gd/(v*v)
        // angle of reach is the angle a at which a projectile must be launced
        // in order to go a distance d, given the initial velocity v.
        float projectileVelocity =
            Mathf.Sqrt(targetDistance / (Mathf.Sin(2 * launchAngle * Mathf.Deg2Rad) / gravity));

        //Extract the x and y component of the velocity
        float vX = projectileVelocity * Mathf.Cos(launchAngle * Mathf.Deg2Rad);
        float vY = projectileVelocity * Mathf.Sin(launchAngle * Mathf.Deg2Rad);

        // Calculate flight Duration(time)
        float flightDuration = targetDistance / vX;

        // Rotate projectile to face the target.
        // Assign the direction the projectile will look
        // LookRotation first argument is forward(the direction to look in)
        myProjectile.transform.rotation =
            Quaternion.LookRotation(targetEntity.transform.position - myProjectile.transform.position);

        Takeable takeable = myProjectile.GetComponent <Takeable>();

        float elapseTime = 0;

        while (elapseTime < flightDuration && !takeable.StopMove)
        {
            //Debug.Log("Flying ball");
            // while flying
            // the components of the velocity of the objects
            // horizontal component remains unchanged v*cosa
            // vertical component changes linearly, because the acceleration due to the gravity is constant.
            // it will be v*sina - gt
            myProjectile.transform.Translate(0, (vY - (gravity * elapseTime)) * Time.deltaTime,
                                             vX * Time.deltaTime);

            elapseTime += Time.deltaTime;

            //wait until next frame... which means no wait!
            yield return(null);
        }

        //yield return new WaitForSeconds(1.0f); //we don't want it to be next frame
    }
コード例 #2
0
 private void FireBomb()
 {
     GameObject projectileObject         = GameObject.Instantiate(bomb, Vector3.zero, Quaternion.identity);
     RangedMonsterBombManager targetbomb = projectileObject.GetComponent <RangedMonsterBombManager>();
     //targetbomb.Launch(this);
 }