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.
“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;
}
}
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: