How to teleport player to an Instantiated Prefab
Hey guys, Year 1 game dev here making my first game for study. Basis of the game is to throw a ball that teleports you either after 1 second or when Space key is pressed. Right now I can't get the player to teleport to the ball on KeyPress and I'm not sure why.
Here is my current throwing script
Rigidbody rb;
[SerializeField]
public GameObject prefab;
public int forceAmount;
public float throwSpeed;
public bool abletothrow;
public GameObject player;
// public GameObject instance;
GameObject ball;
void Start()
{
// GameObject instance = Instantiate(prefab);
// teleBall = Instantiate (obj);
// teleBall.SetActive(false);
prefab.gameObject.GetComponent<DoubleColliderToss>().player = gameObject;
}
IEnumerator WaitForThrow()
{
// suspend execution for 5 seconds
yield return new WaitForSeconds(1);
abletothrow = true;
Debug.Log("Coroutine WaitForThrow worked");
}
void Update()
{
if (abletothrow == false)
{
print("Cannot throw");
}
if (Input.GetButton("Fire1"))
{
throwSpeed += 0.1f; //lowers charge up time
if (throwSpeed >= 15)
{
throwSpeed = 15;
Debug.Log("Charging Celestial");
}
}
if (Input.GetKey(KeyCode.Space))
{
player.transform.position = gameObject.transform.position;
Debug.Log("Space input works");
}
if (abletothrow == true && (Input.GetButtonUp("Fire1")))
{
GameObject teleBall = Instantiate(prefab);
abletothrow = false;
StartCoroutine("WaitForThrow");
teleBall.transform.position = transform.position + Camera.main.transform.forward * forceAmount;
rb = teleBall.GetComponent<Rigidbody>();
rb.velocity = Camera.main.transform.forward * throwSpeed;
throwSpeed = 5;
Sorry for messy code, I am still learning. And here is my teleportation script:
{
public GameObject player;
Rigidbody rb;
// CapsuleCollider capsule;
bool tossed = false;
void Start()
{
//capsule = GetComponent<CapsuleCollider>();
}
// IEnumerator Tele()
// {
// if (Input.GetButton(KeyCode.Space))
//
// {
//
// }
// yield return new WaitForSeconds(1);
// player.transform.position = transform.position;
// Destroy(gameObject);
// // capsule.enabled = false;
// }
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player") return;
if (other.tag == "BouncePlatform") return;
//checks if collider is player or platform that cannot be teleported to
tossed = true;
//capsule.enabled = true; //capsule collider thats the same size as player activates on collision to check if player can fit
// Debug.Log("Capsule is enabled");
// Time.timeScale = 0;
}
void OnCollisionStay(Collision col)
{
if (tossed)
{
tossed = false;
player.transform.position = transform.position;
Destroy(gameObject);
}
}
}
Okay I got space to teleport whenever I want but it teleports me to a set position in my scene ins$$anonymous$$d of wherever the ball is...
Code: if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.Space)) { player.transform.position = prefab.transform.position; Debug.Log("Space input works");
}
Answer by Kossuranta · Dec 15, 2016 at 10:01 AM
Currently you are moving player to same exact position where it already was. GameObject is type of variable and gameObject is reference to the object where you script is attached to. So basically whole variable Player is pretty much useless as you can just use gameObject directly and same works with transform too.
Then in your comment you moved player to the position of prefab, meaning the position the prefab happens to have in Assets folder. If you click it you will see that it has some position in Inspector.
You have created GameObject ball, but I couldn't see that you use it anywhere. So you could just rename that to GameObject teleBall.
Then change this
GameObject teleBall = Instantiate(prefab);
to this
teleBall = Instantiate(prefab) as GameObject;
Now you create new object that is copy of prefab and assign it to variable teleBall. I'm not actually sure that do you need to use as GameObject, but I think you do as it will give you error otherwise.
Then you can move the player to the position of the teleBall that you created like this:
transform.position = teleBall.transform.position;
transform with small t is reference to the Transform of the object the script it attached to so we don't need the player variable at all.
Your answer
Follow this Question
Related Questions
how do i instantiate multiple object object with line change with for loop. 1 Answer
Prefab Enemy2 to track all prefab Enemy1's Instantiates transforms: How? 1 Answer
Having problem with instantiated prefabs 0 Answers
Instantiating prefabs, and making them move. (Projectiles) 0 Answers
How can I pass a reference to an Instantiated object? 0 Answers