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).
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;
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
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...
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): class Projectile : Monobehaviour { public float speed = 10.0f; public Vector3 Direction{get;set;} void Update() { transform.position += Direction * (Time.deltaTime * speed); } } Then when instantiating: Code (CSharp): Projectile bullet = Instantiate(bolt, shotSpawn.position, Quaternion.Identity) as Projectile; bullet.Direction =( interceptPoint - shotSpawn.position).normalized;
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;''..
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
Yes, sure. Code (CSharp): using UnityEngine; using System.Collections; class Mover : MonoBehaviour { public float speed=1.1f; public GameObject explosion; public float lifeTime; public Vector3 Direction{get;set;} private bool destroyed=false; void OnTriggerEnter(Collider other){ if (other.tag == "Blue Team") { return; } Destroy (gameObject); Instantiate (explosion, transform.position, transform.rotation); destroyed = true; } void Update(){ if (!destroyed && Time.time>=lifeTime) { Instantiate (explosion, transform.position, transform.rotation); Destroy (gameObject); } transform.position += Direction * (Time.deltaTime * speed); } void Start () { GetComponent<Rigidbody>().velocity = transform.forward * speed; lifeTime +=Time.time; } } 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): Mover bullet = Instantiate(bolt, shotSpawn.position, Quaternion.identity) as Mover; bullet.Direction = (interceptPoint - shotSpawn.position).normalized; Quaternion.Identity doesn't exist, that's why I wrote Quaternion.identity.