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 churu · Apr 14, 2016 at 07:59 PM · vector3teleport

Creating a new vector3

So, i'm having a object instantiated that if collided with will teleport you. To find the player position and define it i use the following code

using UnityEngine; using System.Collections;

public class BlackHole : MonoBehaviour {

public float Player;

 // Use this for initialization
 void Start () {
     Player = GameObject.Find("Player").transform.position.y;
 }
 
 // Update is called once per frame
 void Update () {
 
 }

 void OnTriggerEnter2D(Collider2D col) 
 {
     if (col.gameObject.tag == "Player") 
     {
      
     }
 
 }

}

But when i try and set a new vector in the trigger i get an error i cannot convert a float to vector3.

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 gjf · Apr 14, 2016 at 09:03 PM 2
Share

show the code that generates the error. show the error too...otherwise we're guessing.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Blue-Cut · Apr 14, 2016 at 09:15 PM

"Cannot convert a float to vector3" means that you are trying to affect a float to a vector3. You are doing something like :

 Vector3 position = myFloat;

I am not sure what you are trying to do but :

 // This is the position you want to give to your player if it collides with your blackhole
 public Vector3 teleportPosition;

  //This is the collision method of the blackhole script
  void OnTriggerEnter2D(Collider2D col) 
  {
      // If the tag is player, col is the collider of the player
      if (col.gameObject.tag == "Player") 
      {
            // So we give to the player the public position set above
           col.transform.position = teleportPosition;
      }
  }
Comment
Add comment · Show 3 · 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
avatar image churu · Apr 19, 2016 at 12:17 PM 0
Share

I am trying to teleport the player from enter point A to exit point B, the black hole is a prefab so i cannot use a rigidbody2D to find its position.

avatar image Blue-Cut churu · Apr 19, 2016 at 12:23 PM 0
Share

I don't think that rigidbody has something to do here. So if I understand, your player collides with a black hole, and the collision teleport you to... another black hole or a given position ? I am going to edit my answer in this sense.

avatar image churu · Apr 19, 2016 at 02:45 PM 0
Share

yes it works. thanks!

avatar image
0

Answer by Jessespike · Apr 14, 2016 at 09:10 PM

You can use the "col" as the player, since you check if it's the player anyway. I'd also recommend not storing Player as a float, either use a GameObject or a Transform. Or better yet, just remove it completely.

In the OnTriggerEnter2D function, you can do something like this:

 col.transform.position = new Vector3(0f, 0f, 0f);

or

 col.transform.position.Set(0f, 0f, 0f);

Unity Tutorials

Vector3

Comment
Add comment · Show 2 · 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
avatar image Bunny83 · Apr 15, 2016 at 04:39 AM 1
Share

This line:

 col.transform.position.Set(0f, 0f, 0f);

doesn't work. position is a property. This line will invoke the getter of the property and return a Vector3 struct. Calling "Set" on that returned value won't change the actual position. In order to change the position the setter has to be invoked. This can only be done by assigning a Vector3 to the position property.

avatar image churu · Apr 19, 2016 at 12:13 PM 0
Share

@jessespike I cannot use it as a rigidbody or object since the stored information is on a prefab. So somehow i need to find the position of the player through code then change it

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 to teleport an object to 1 of 8 possible locations on collision? 1 Answer

Adding a Vector to a position 2 Answers

Teleport camera to where is pointing at 3 Answers

Movement Sticking 0 Answers

rotate object around a point over time 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