Instantiating a Rigidbody prefab

Hello guys !

I have the following code, which generetes a Vector and reflects a new one.

if (Physics.Raycast(rayEmitter.position, FirstRay * distance, out hit0, Mathf.Infinity, PrefabMask))
        {
                if (hit0.collider.gameObject.tag == "mirror")
                {
                    
                    Vector3 incidenceAngle0 = hit0.point - rayEmitter.position;
                    Vector3 reflectionAngle0 = Vector3.Reflect(incidenceAngle0, hit0.normal);
                    target0 = hit0.transform;
                    Debug.DrawRay(hit0.point, reflectionAngle0*distance, Color.white);

                   // timer += Time.deltaTime;
                   // if (timer >= timeStep)
                   // {
                        timer = 0F;
[COLOR="blue"]                        Quaternion rot2 = Quaternion.identity;
                        Rigidbody beamHeader2 = GameObject.Instantiate(beamHeader, hit0.point, rot2) as Rigidbody;
                        beamHeader2.AddForce(reflectionAngle0 *rayDistance);
                        beamHeader2.detectCollisions = false;[/COLOR]
                   // }
                    if (Physics.Raycast(target0.position, reflectionAngle0 * distance, out hit1, Mathf.Infinity, PrefabMask))
                    {
                            if (hit1.collider.gameObject.tag == "mirror")
                            {
                                Vector3 incidenceAngle1 = hit1.point - target0.position;
                                Vector3 reflectionAngle1 = Vector3.Reflect(incidenceAngle1, hit1.normal);
                                target1 = hit1.transform;
                                .....

I’m trying to instantiate a prefab w/ a rigidbody and a trail render to make the beam “trail”. When the rayDistance var is 1, this happens:


So, as you see, because the force is very weak, the projectile isn’t launched properly, thus not leaving the beam trail.
Now, when I change the rayDistance var to lets say, 300, this is what happens:

Despite launching the projectile with proper force now, the projectile isn’t created where it should be. Is there a way to fix this?

Thanks in advance !

The AddForce function has an optional second parameter of type ForceMode which allows you to change the way the force is applied to the rigidbody. If you use the Impulse or VelocityChange modes, the object will get a sudden jolt rather than a gradual push to accelerate it.

thanks, I’ll try it !

And this is why I am now trolling the scripting boards:). Thanks for the input andeeee

Looking at the screenshot, the texture appears to have transparent zones around the edges. As a result, the object is most likely starting at the right point, but you can’t see it easily when it spreads out and the transparent zone gets larger.

If you want a true beam, you should use a texture that’s opaque from left to right.

Gargerath, what happens is, when I use RigidBody.AddForce(reflectionAngle0), the prefab is created at the right position, but with weak force. If I multiply the Vector for a number, to increase the force, it also changes its starting position. It’s hard to spot on the screenshot, but when the prefab had a fire texture, it was clearly visible. I’ll see if the ForceMode helps, now.