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 /
avatar image
0
Question by raralphhh · Sep 30, 2016 at 09:39 AM · gameobjectprefabteleportteleporting

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??

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 LeonardNS · Sep 30, 2016 at 11:44 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


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