playMaker

Author Topic: Throwing object[SOLVED]  (Read 1540 times)

El_Toro

  • Playmaker Newbie
  • *
  • Posts: 7
Throwing object[SOLVED]
« on: May 11, 2017, 05:32:51 PM »
Hi everyone

Trying to accomplish something here and hit a wall...

I'm looking to have an object thrown to another object using some trajectory settings (Think quarterback to receiver). I've accomplished that part. What I want is for the object throwing the ball (quarterback) to throw towards the other object (receiver) as the receiver is moving. Basically, the quarterback anticipating the receiver position based on trajectory and velocity, in order to place the thrown object in the anticipated location.

Like I mentioned, I have it where it will throw to it in a static location. Just want it to be dynamic. Hoping that someone can point me in the right direction.

Here is the code I have. Would be great if I could do it using Playmaker:


Code: [Select]
using UnityEngine;
using System.Collections;
 
public class ThrowSimulation : MonoBehaviour
{
    public Transform Target;
    public float firingAngle = 45.0f;
    public float gravity = 9.8f;
 
    public Transform Projectile;     
    private Transform myTransform;
   
    void Awake()
    {
        myTransform = transform;     
    }
 
    void Start()
    {         
        StartCoroutine(SimulateProjectile());
    }
 
 
    IEnumerator SimulateProjectile()
    {
        // Short delay added before Projectile is thrown
        yield return new WaitForSeconds(1.5f);
       
        // Move projectile to the position of throwing object + add some offset if needed.
        Projectile.position = myTransform.position + new Vector3(0, 0.0f, 0);
       
        // Calculate distance to target
        float target_Distance = Vector3.Distance(Projectile.position, Target.position);
 
        // Calculate the velocity needed to throw the object to the target at specified angle.
        float projectile_Velocity = target_Distance / (Mathf.Sin(2 * firingAngle * Mathf.Deg2Rad) / gravity);
 
        // Extract the X  Y componenent of the velocity
        float Vx = Mathf.Sqrt(projectile_Velocity) * Mathf.Cos(firingAngle * Mathf.Deg2Rad);
        float Vy = Mathf.Sqrt(projectile_Velocity) * Mathf.Sin(firingAngle * Mathf.Deg2Rad);
 
        // Calculate flight time.
        float flightDuration = target_Distance / Vx;
   
        // Rotate projectile to face the target.
        Projectile.rotation = Quaternion.LookRotation(Target.position - Projectile.position);
       
        float elapse_time = 0;
 
        while (elapse_time < flightDuration)
        {
            Projectile.Translate(0, (Vy - (gravity * elapse_time)) * Time.deltaTime, Vx * Time.deltaTime);
           
            elapse_time += Time.deltaTime;
 
            yield return null;
        }
    } 
}
 
 

Thanks for any help you can give me.

Robert
« Last Edit: September 26, 2017, 04:17:55 AM by jeanfabre »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Throwing object
« Reply #1 on: September 26, 2017, 04:17:44 AM »
Hi,

 there is a sample on the Ecosystem that demonstrate the drag and throw behaviour.

https://twitter.com/JeanAtPlayMaker/status/912575129569435648

 bye,

 Jean