Optimizing My Teleportation Gun?

Hi, I need to optimize my teleportation gun in a few ways. Is there a way I can make it so that the instantiated object is affected differently to the original object? The gun is also pretty buggy, including where the original bullet spawns.

Gun Code:

#pragma strict

var bullet : Rigidbody;
var bulletPoint : Transform;
var bulletInstance : Rigidbody;

function Update () 
{
	var power : int = 500;
	var hit : RaycastHit;    
          
    if (Input.GetButtonDown("Fire1"))
    {
        var fwd: Vector3 = transform.TransformDirection(Vector3.forward);

        if (Physics.Raycast (transform.position, fwd))//Check if raycast hit
            print ("Raycast Hit!");
        else
            print ("Raycast Didn't Hit!");

        var bulletInstance: Rigidbody = Instantiate(bullet, transform.position, transform.rotation) as Rigidbody; //Object to clone, origin of bullet, rotation
        bulletInstance.AddForce(fwd * power); //Moving projectile
        Quaternion.LookRotation(hit.normal);
        
    }
}

Teleport Script:

var target : Transform;

function OnTriggerEnter( col : Collider )
{
	player = GameObject.Find("Player");
	
	if(col.gameObject.tag == "teleport")
	{
		player.transform.position = target.position;
	}
}

Preview: http://i.imgur.com/QR7ESdZ.png

Any optimization help would be great.

hmm…

  1. dont raycast when shooting… what is it for??
  2. dont instantiate bullets, use objects pool instead (there are many examples how to implement pool, you could use one of them or make your own).
  3. btw, i dont see where do you destroy bullets after hit… anyway, if you would use object pool - you will not destroy bullet but deactivate and re-parent it instead.
  4. Dont use Find(“player”) in bullet, link player to target after instantiate it from player script.

PS to be honest, your code is quite messy -)))Try first to describe the task in detail first, what each object should do… but it comes with practice :slight_smile: