
I need a trail of prefabs which shows the directory of the flight of my projectile.
Here’s my code:
using UnityEngine;
using System.Collections;
public class bang : MonoBehaviour {
public Rigidbody rb;
public float jumpPressure;
public bool canshoot;
public bool canbomb;
public Transform door;
public GameObject booms;
public float timer;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody>();
canshoot = false;
canbomb = false;
}
// Update is called once per frame
void Update () {
if (Input.GetButtonUp ("Fire1"))
{
rb.AddForce (transform.forward * jumpPressure * 2);
rb.AddForce (Vector3.up * jumpPressure);
canshoot = true;
canbomb = true;
}
if (timer > 0)
{
timer -= Time.deltaTime * 38f;
}
if (timer <= 0 && canbomb)
{
Instantiate (booms, door.position, Quaternion.identity);
timer = 2;
}
}
void OnCollisionEnter(Collision other)
{
if (other.gameObject.CompareTag ("ground") && canshoot) {
rb.isKinematic = true;
canbomb = false;
}
}
}
As you can see I’m using a timer to determine the instantiation of my little spheres prefabs. I’m pretty happy with that, but how do set it so that there won’t be any space between those little blue sphere? If make the timer too short, they start bumping into each other, and that’s what I don’t want.
Another question: can I somehow change the speed of the flight of my purple sphere?
I was trying to slow time – but it looks more like a lag, that’s not what I need.
Thanks!
For blue spheres, you could simply check the distance using Vector3.Distance() between purple sphere and last sphere (or start point if first sphere doesn’t exist).
if distance >= (blue sphere diameter + blue sphere radius + purple sphere radius) => spawn blue sphere at last sphere (or point) + blue sphere diameter and then set it as last.
This produce perfect results regardless or move speed.
I don’t understand everything in your code (comments are cruely missing ! Think about it in the future !!) You may need to do some tweaks with the following code :
using UnityEngine;
using System.Collections;
public class bang : MonoBehaviour {
public Rigidbody rb;
public float jumpPressure;
public bool canshoot;
public bool canbomb;
public Transform door;
public GameObject booms;
// Adjust value here, or let the last line of the Start function do the job
public float distance ;
private Vector3 lastInstantiationPoint ;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody>();
canshoot = false;
canbomb = false;
// Optionnal, you can adjust it, with a coefficient for example
distance = 2 * booms.GetComponent<MeshRenderer>().bounds.size.x ;
}
// Update is called once per frame
void Update () {
if (Input.GetButtonUp ("Fire1"))
{
rb.AddForce (transform.forward * jumpPressure * 2);
rb.AddForce (Vector3.up * jumpPressure);
canshoot = true;
Instantiate (booms, door.position, Quaternion.identity);
lastInstantiationPoint = door.position ;
}
if( Vector3.Distance( lastInstantiationPoint, transform.position ) > distance && canbomb )
{
Instantiate (booms, door.position, Quaternion.identity);
lastInstantiationPoint = door.position ;
}
}
void OnCollisionEnter(Collision other)
{
if (other.gameObject.CompareTag ("ground") && canshoot) {
rb.isKinematic = true;
canbomb = false;
}
}
}
Your problem is : When you call Instantiate it take time and that time depend on your CPU usage.
Second problem is : Time.deltime is not equal all the time, it’s normal but if your timer is equal to 0.00001 it will drecrease under 0 with Time.deltime, so it takes 2seconds + Time.deltatime.
If your timer is equal to 0 + Time.deltatime so it will instantiate to 2seconds.
Third problem is windows, you dont know when the scheduler will give your processus the priority to run.
So for me there is only three method, use TrailRender(or Particle) of Unity. Or when you instantiate your sphere try to set his position at the last position sphere + his diameter. Or you disable collision between sphere and you instantiate mass.
But i prevent you, Instantiate is heavy performance so dont use the last solution. The best for me is TrailRenderer(or Particle)