Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Catbug-Sqeagy · Dec 15, 2016 at 08:29 AM · instantiateprefabtransform.position

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);
 
 
 
         }
     }
     
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Catbug-Sqeagy · Dec 15, 2016 at 05:28 AM 0
Share

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");

        }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges