- Home /
Cannot move gameObject without having particles emitted at wrong locations
The green lines are the raycasts - depending on the hit.point I emit particles at that point. This works well when the object is at (0,0,0), but moving it, for example 5 in x axis results in this:
5 rings of particles, twice as far. This is the main part of code which handles this:
public IEnumerator Scan(){
for(int i = 0; i <= 180; i++){
origin = new Vector3(Mathf.Cos(i * (Mathf.PI/180f)),0f,Mathf.Sin(i * (Mathf.PI/180f)));
for(int x = 0; x <= density; x++){
quat = Quaternion.Euler(x,0,0);
sphere[x].transform.localPosition = quat*origin;
Vector3 heading = sphere[x].transform.parent.position - sphere[x].transform.position;
float distance = heading.magnitude;
Vector3 direction = heading / distance;
Debug.DrawRay(sphere[x].transform.position, direction, Color.green, 0.02f);
if(Physics.Raycast(sphere[x].transform.position, direction, out hit) && hit.collider.gameObject.name != null){
vibration_particle_system.Emit(hit.point, sphere[x].transform.parent.position, 0.05f, 0.1f, Color.blue);
}
}
yield return new WaitForSeconds (0.01f);
}
}
Now, why does this happen? How can I fix this? The raycasts seem to be doing fine - they appear where I want them to and retrieve the points of contact with the object collider. Why do the particles not behave as they should? Thank you.
This is the full slightly edited code:
void OnCollisionEnter(Collision collision) {
ContactPoint contact = collision.contacts[0];
contact_point = contact.point;
base_object.transform.rotation = Quaternion.FromToRotation(Vector3.right, contact_point);
impact_force = Vector3.Dot(collision.contacts[0].normal,collision.relativeVelocity) * rigidbody.mass;
vibration_particle_system = /*GameObject.Find(this.gameObject.name)*/particle_system_holder.AddComponent("ParticleSystem") as ParticleSystem;
StartCoroutine(Scan());
}
public IEnumerator Scan(){
for(int i = 0; i <= 180; i++){
origin = new Vector3(Mathf.Cos(i * (Mathf.PI/180f)),0f,Mathf.Sin(i * (Mathf.PI/180f)));
for(int x = 0; x <= density; x++){
quat = Quaternion.Euler(x,0,0);
sphere[x].transform.localPosition = quat*origin;
Vector3 heading = sphere[x].transform.parent.position - sphere[x].transform.position;
float distance = heading.magnitude;
Vector3 direction = heading / distance;
Debug.DrawRay(sphere[x].transform.position, direction, Color.green, 0.02f);
if(Physics.Raycast(sphere[x].transform.position, direction, out hit) && hit.collider.gameObject.name != null){
vibration_particle_system.Emit(hit.point, new Vector3(0,0,0), 0.05f, 0.1f, Color.blue);
}
}
yield return new WaitForSeconds (0.01f);
}
}
I assume this line is to be fixed first:
base_object.transform.rotation = Quaternion.FromToRotation(/*Vector3.right*/ new Vector3(1,0,0), contact_point);
I'm wondering several things:
When you move it, after a few moments does the position of the particles start popping out of the right place? I mean does it catch up like it should be when it's stationary again?
Or does it just forever emit from an incorrect location like this?
Why use localPosition ins$$anonymous$$d of regular old position? Also why do you multiply the localPosition assignment like that (the quat times origin part) ?
What exactly is the math deciding the variable quat for (I mean what's the desired result of that math)? Why iterate through i 180 times, and what's density? Whats the sphere[] variable?
I can't understand exactly what this codes supposed to do based on this math and stuff is why I ask all these questions lol. Is the goal just a trail following the cubes movement always? Or should this be triggered at some point, play, then stop?
I changed the speed variable in Emit() to zero, so now there is only one ring, but twice as far. The purpose of this code is to check where an object was hit and then create a sphere of raycast around the object, returning hit points, where the rays intersect with the collider. At those points a particle system emits particles to make a blue ring. It all works wonderfully when the gameObject is at the center of the coordinates. But moving it just breaks everything. Furthermore, moving the object results in the ray sphere drawing from a fixed position, it doesn't rotate at all. I'll update the question with the full code.
and to the questions - the particles do not catch up, the base_object (it gives the green scan) doesn't rotate to match the point of collision from another object, localPosition is used to ease the drawing of a sphere, because later I need to rotate that cutie, quat*origin gives a sphere shape. Density is just 360 now. sphere[] is a list of all the points in the sphere from where the green line starts and shoots to the center.
Your answer
Follow this Question
Related Questions
SphereCastAll doesn't works 1 Answer
Physics.SphereCastAll with triggers 1 Answer
Raycasting onto a sphere 1 Answer
How to get Raycast surface normal to align particle system? 1 Answer
Obstacle Avoidance 0 Answers