Question by
Jerry1797 · May 04, 2017 at 02:31 AM ·
collider2d gametriggertransform.positionteleporting
Having trouble teleporting player in a 2D game
Pretty new to programming so I'm sure I made a few errors here. I am trying to make it so when the player walks into the edge of the map they hit a collider trigger that teleports them to the same spot on the other side of the map effectively making the map go on forever.
The errors I'm getting are:
error CS0029: Cannot implicitly convert type 'void' to 'float'
error CS0236: A field initializer cannot reference the nonstatic field, method, or property 'UnityEngine.Component.transform'
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TeleportY : MonoBehaviour {
public float v = transform.position.x;
public float h = transform.position.y;
void OnTriggerEnter2D(Collider2D other)
{
if (v < 0)
{
v = Debug.Log (Mathf.Abs (v));
transform.position = new Vector2 (h, v);
}
if (v > 0)
{
v = v * -1;
transform.position = new Vector2 (h, v);
}
}
}
Thanks in advance for the help!
Comment
Your answer
Follow this Question
Related Questions
Problem with 2D Triggers 1 Answer
Audio Trigger 2D in C#? 1 Answer
AI and Enemy Detection 1 Answer
Change Scene with Physics Raycasting 1 Answer
How to Destroy A GameObject when it enters any Triggers 2 Answers