Search Unity

Calculating a leading shoot in C#

Discussion in 'Scripting' started by Toni95, May 21, 2015.

  1. Toni95

    Toni95

    Joined:
    Apr 15, 2015
    Posts:
    7
    Hello Unity community.

    I would really appreciate any help anyone could provide me, so if you are reading this let me thank you so much.

    Ok, I want to shoot to a mobile target, but not to its current target.transform.position, I need to shoot it to an intercept position between the projectile and the target, so I will hit it if it moves in a constant speed and direction. How should I do that using "Instantiate(bolt,shotSpawn.position,shotSpawn.rotation)"?

    We know the distance between the 2 objects every time.
    The projectiles have a constant speed and a lifetime of 2 seconds (as maximum).
     
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
  3. Toni95

    Toni95

    Joined:
    Apr 15, 2015
    Posts:
    7
    Thanks for your answer James, but still I have some questions.

    How can I translate this to C#?:

    //velocities
    Vector3 shooterVelocity = shooter.rigidbody ? shooter.rigidbody.velocity : Vector3.zero;
    Vector3 targetVelocity = target.rigidbody ? target.rigidbody.velocity : Vector3.zero;
     
  4. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    That is C#.

    1 thing though, you can't use target.rigidbody property anymore so you will need to do a target.GetComponent<RigidBody>() instead
     
  5. Toni95

    Toni95

    Joined:
    Apr 15, 2015
    Posts:
    7
    Thanks for your help, I almost done it, I just need a way to shoot to that Vector3 position. I just know how to use Instantiate, and I think that you can't do it using that...

    That's what I have tried:

    Quaternion aux = Quaternion.LookRotation (interceptPoint);
    Instantiate(bolt, shotSpawn.position, aux);

    But this is not working, it just shoots to a crazy position...
     
  6. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    If you create a script called: Projectile.cs or something similar and attach it to your bolt.prefab you can do something like this:

    Code (CSharp):
    1. class Projectile : Monobehaviour
    2. {
    3.  
    4.    public float speed = 10.0f;
    5.    public Vector3 Direction{get;set;}
    6.  
    7.    void Update()
    8.   {
    9.     transform.position += Direction * (Time.deltaTime * speed);
    10.   }
    11.  
    12.  
    13. }
    Then when instantiating:

    Code (CSharp):
    1. Projectile bullet = Instantiate(bolt, shotSpawn.position, Quaternion.Identity) as Projectile;
    2.  
    3. bullet.Direction =( interceptPoint - shotSpawn.position).normalized;
     
  7. Toni95

    Toni95

    Joined:
    Apr 15, 2015
    Posts:
    7
    I had a script attached to the bolt with that code:

    GetComponent<Rigidbody>().velocity = transform.forward * speed;

    I'm trying what you told me but I'm getting a NullReferenceException error and the bolt is being instantiated but it's not moving. The errors are located in functions that reach the function where I calculate the interceptPoint and Instantiate the bolt, and in the line where I have ''bullet.Direction = (interceptPoint - shotSpawn.position).normalized;''..
     
  8. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Can you show the code attached to the bolt? I see now what you were trying to do with setting the initial rotation to look at the intercept point
     
  9. Toni95

    Toni95

    Joined:
    Apr 15, 2015
    Posts:
    7
    Yes, sure.
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. class Mover : MonoBehaviour
    6. {
    7.     public float speed=1.1f;
    8.     public GameObject explosion;
    9.     public float lifeTime;
    10.     public Vector3 Direction{get;set;}
    11.  
    12.     private bool destroyed=false;
    13.  
    14.     void OnTriggerEnter(Collider other){
    15.         if (other.tag == "Blue Team") {
    16.  
    17.             return;
    18.         }
    19.         Destroy (gameObject);
    20.         Instantiate (explosion, transform.position, transform.rotation);
    21.         destroyed = true;
    22.     }
    23.  
    24.     void Update(){
    25.         if (!destroyed && Time.time>=lifeTime) {
    26.             Instantiate (explosion, transform.position, transform.rotation);
    27.             Destroy (gameObject);
    28.         }
    29.         transform.position += Direction * (Time.deltaTime * speed);
    30.     }
    31.  
    32.     void Start ()
    33.     {
    34.         GetComponent<Rigidbody>().velocity = transform.forward * speed;
    35.  
    36.         lifeTime +=Time.time;
    37.     }
    38. }
    39.  
    I tried removing ''GetComponent<Rigidbody>().velocity = transform.forward * speed;'', but this is not causing the error.
    And that's what I wrote in the shoot script:
    Code (CSharp):
    1.  
    2. Mover bullet = Instantiate(bolt, shotSpawn.position, Quaternion.identity) as Mover;
    3. bullet.Direction = (interceptPoint - shotSpawn.position).normalized;
    4.  
    Quaternion.Identity doesn't exist, that's why I wrote Quaternion.identity.
     
    Last edited: May 31, 2015