- Home /
Teleport to the position of the prefab
hi can anyone help me. so im trying to teleport to the object i shoot but what happens is that i keep on teleporting to the same spot where i deleted the prefab object
public GameObject myPlayer; public GameObject OrbPrefab; public Rigidbody orb;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//teleport
Teleporting();
}
void Teleporting()
{
if(Input.GetKeyDown(KeyCode.E))
{
GameObject teleporter = (GameObject)Instantiate(OrbPrefab,Vector3.one,Quaternion.identity);
Vector3(transform.position.x,transform.position.y,transform.position.z);
transform.position = OrbPrefab.transform.position;
Destroy(teleporter);
}
}
her is the shoot
public Transform orb; Transform orbshoot;
private Vector3 mouse_pos;
public Transform target;
private Vector3 object_pos;
private float angle;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
mouse_pos = Input.mousePosition;
mouse_pos.z = -10;
object_pos = Camera.main.WorldToScreenPoint(target.position);
mouse_pos.x = mouse_pos.x - object_pos.x;
mouse_pos.y = mouse_pos.y - object_pos.y;
angle = Mathf.Atan2(mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0, 0, angle);
if(orbshoot == null)
if(Input.GetMouseButtonDown(0))
{
orbshoot = Instantiate(orb) as Transform;
orbshoot.transform.position = transform.position + Camera.main.transform.right * 2;
Rigidbody rb = orbshoot.GetComponent<Rigidbody>();
rb.velocity = Camera.main.ScreenToWorldPoint(orb.position) * Time.deltaTime;
Destroy(orbshoot.gameObject,2.0f);
}
}
}
can anyone tell me why??
I think one of your mistakes is on line 33 in the first script. You are taking the position from the Prefab itself, not the instance you created in the other script. You should find a way to reference the other object and get the real "shoot" ins$$anonymous$$d of the prefab.
Answer by Astrydax · Oct 01, 2016 at 12:22 AM
What does this line of code even do?Vector3(transform.position.x,transform.position.y,transform.position.z);
What you can do is just change this
void Update () {
//teleport
Teleporting();
}
Into this
bool readyforteleport = false;
void Update () {
//teleport
If(readyforteleport)
Teleporting();
}
In your shoot method whenever you're ready to begin teleporting set readyforteleport to true. Then at the end of your Teleporting() method set readyforteleport to false.
This will only call it once each time its enabled.
You also need to be sure that you're getting a reference to the instantiated game object and not the prefab. Its not the best practice but using gameobject.find will do the trick.
Your answer
Follow this Question
Related Questions
How can I tell what prefab a GameObject belongs to? 2 Answers
Spawn Prefabs 1 Answer
Creating an array of prefabs? 4 Answers
Replacing empty gameobjects with prefabs 3 Answers
How can I spawn a bullet using PhotonNetwork.Instantiate()? 1 Answer