- Home /
Cannot use triggers to teleport an object?
This is my script:
using UnityEngine;
using System.Collections;
public class screenbottom : MonoBehaviour {
void OnTriggerEnter(Collider2D col)
{
if(col.gameObject.tag == "screenbottom")
{
transform.position (GameObject.FindWithTag ("screentop"));
}
}
}
The end goal is to enable an object to fall off the bottom of the screen and continue on through to coming back down from the top infinitely. I have set 'screenbottom' and 'screentop' as 'is Trigger', given them both rigidbody2d and coinciding tags. I cannot imagine why this isn't working but I am new to much of this. Perhaps using entering triggers is not the best idea in the first place? Due to the specificness of using one spawn-point, I can't even imagine how one would expand on this script to allow for the players position to still continue forwards while falling. Any help is super appreciated.
Awesome that's definitely gotta be it, the error I was getting before was almost exactly what you said- but now I am still getting one exception: " The type or namespace name `Gameobject' could not be found. Are you missing a using directive or an assembly reference? " And at the risk of sounding too ignorant, in the code you gave me it is still assumed that the standard C# initialization lines,
using UnityEngine;
using System.Collections;
public class screenbottom : $$anonymous$$onoBehaviour {
are still to be included at the top? The only other thing I could think of is, this script is supposed to be attached to a rigidbody2d game object tagged 'Brick'- now that shouldn't be in the code now should it?
I'm sorry about that, i have changed my answer to be correct, and it should be easy to just copy and use! Just comment again if there is an issue!
Holy cow that works like a charm, and using the editor to add the transform in really makes the code modular and easy to use. Now I can dissect it and start doing all kinds of fun things. Hope to be a useful member of the community sometime soon here! I posted the solution any other forums that I asked this on so that the solution is out there.
Also I found that using linear drag seems to be an appropriate way of capping the y velocity of the object from having it's speed increase infinitely.
Cheers!
P.s. and sorry moderators for my accidentally trying to respond with an answer ins$$anonymous$$d of a comment again!
and I will delete this comment as soon as someone tells me this, but how do I give rob5300 more $$anonymous$$arma points or whatever it is for the awesome answer? He deserves it and I am apparently inept!
@buttholejohnson:
If you want to tick an answer as solution, just click the checkmark:
You can't upvote his answer since you don't have enough karma yourself. You need at least 15 karma to vote. This is to prevent spam accounts to push each other.
Answer by rob5300 · Nov 16, 2014 at 10:49 PM
You are trying to use transform.position as a method which doesn't exist.
To change the position of the game object that this script is connected to that object should be done like this:
using UnityEngine;
using System.Collections;
public class screenbottom : MonoBehaviour {
//This script must be attached to your player that moves.
public Transform screentop;
//This is a public Transform, soo we can assign this object the transform of the screentop
//object in the editor, making it much easier to change!
void OnTriggerEnter2D(Collider2D col) {
//This needs to be OnTriggerEnter2D as we are using 2D colliders
if (col.gameObject.tag == "screenbottom") {
Debug.Log("Screen bottom found, moving position to y: " + screentop.position.y);
//Here we set the y in the transform to the y of the screentop object, thus
//making sure the character can still move along the x axis (soo we set the x as what it already was to get that behaviour)
transform.position = new Vector2(transform.position.x, screentop.position.y);
}
}
}
Your answer
Follow this Question
Related Questions
Change transform.position.y in a loop 1 Answer
transform.position sends object to wrong position 1 Answer
transform position player teleport 0 Answers
Transform position? 2 Answers
Simplist way to rotate camera 180degrees on trigger? 1 Answer