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.
show the code that generates the error. show the error too...otherwise we're guessing.
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;
}
}
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.
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.
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);
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.
@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
Follow this Question
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