Teleport my Character

Hello!

I want to make a 2D game for Android, but I have a problem. I want, if I press the “up” button, my character should teleport up by 10y.
He also should teleport through objects.

e.g: If I am at the position 10,10,10 and I press the “up” button, he should teleport to 10,20,10.

Does anybody know how I can do this?

when your input shows that up was pushed, change the position to the new position (don’t use move, actually change the position of the transform).

There is a problem though if the new location is inside a wall… before moving, I’d do a sphere overlap test at that new position (for the radius of the sphere, use the players bounding sphere). If it overlaps anything, then you’re inside something. Either not do the move, or change the position slightly and retest (where to move to depends on how you want to react if you try and teleport into something… maybe we teleport closer, maybe we teleport farther, maybe not at all).

Assign the player a “player” tag.

In C# -

GameObject PlayerObj = GameObject.FindWithTag("Player");

If Conditions are met, etc. {
    PlayerObj.transform.position = new Vector3(PlayerObj.x,PlayerObj.y += 10,PlayerObj.z);
}

You could also give the player gameobject a name of “Player” and use GameObject.Find(“Player”). Don’t create multiple named objects or tagged objects of “Player” unless you want them all to move. Have fun.

Do remember to do some kind of check to make sure the character isn’t just teleporting into a void or solid ceiling. You could probably use OnCollisionEnter or OnTriggerEnter to accomplish this.

Thx, I have now this code:

if(oberwelt) {
				PlayerObj.transform.position = new Vector3(PlayerObj,PlayerObj += 10,PlayerObj);
		
			}

and I get this 3 errors:
error CS0019: Operator +=´cannot be applied to operands of type ÙnityEngine.GameObject´and ìnt´ error CS1502: The best overloaded method match for ÙnityEngine.Vector3.Vector3(float,float,float)´has some invalid arguments error CS1503: Argument #1´cannot convert ÙnityEnginge.GameObject´expression to type `float´

Do you know what I can do?

You’re only referencing the PlayerObj itself inside your new Vector3. It needs to be PlayerObj.x, .y, and .z respectively.

You can also do “PlayerObj.transform.position.y += 10.0f;” at that point. Rather than modifying the entire position vector3.

since Vector3s, which is what our position variable is, takes three floats, we need to use a float “conversion” on our numeral. 10.0f f is used to make a number a float when assigned, and is the reason you were getting error CS1502, or probably would still have when trying to run this code before this edit… sorry about that :stuck_out_tongue:

BTW, if (upper world?) lol

If I have “(PlayerObj.x,PlayerObj.y += 10,PlayerObj.z);” It says:UnityEngine.GameObject does not contain a definition for “x” and no extensin method “x” of type UnityEngine.GameObject could be found (are you missing a using directive or an assembly reference?)
Same at y and z.

If I have

if(oberwelt) {
				PlayerObj.transform.position.y += 10;
		
			}

i get this error: Cannot modify a vlue type return value of UnityEngine.Transform.position. Consider storing the value in a temporary variable.

Sorry, I must be high… I’m being stupid right now, trying to track down an internal compiler error I’m getting in Unity… anyway.

PlayerObj is equal to your GameObject right? So Your GameObject is literally just the GameObject for your player in the world. It’s not a variable really, it’s just a reference to access a bunch of other variables. transform is a set of variables, including position, scale, and rotation. Position is a Vector3, it takes three inputs, x,y, and z respectively. This is the gameobject’s location, and what we’re accessing.

Why the .transform.position.y += 10f isn’t accessible is beyond me, it’s a Unity coding limitation and is easily worked around by doing something like making a new integer y = PlayerObj.transform.position.y and adding 10 to that.

But to keep things working, go back to the Vector3 solution, and access the position when filling your new Vector3. PlayerObj.transform.position contains your x,y,z of your Player’s current position.

I am also not really smart at the moment.

What should I do know? :slight_smile:

Just change the new Vector3() contents to be PlayerObj.transform.position.x .y .z

Do you mean:

 if(oberwelt) {
				PlayerObj.transform.position = PlayerObj.transform.position.y += 10;
		
			}

Lol, no. x += y is the same thing as x = x + y. It’s just adding the second variable to the first. That line would give you a conversion error since position is a vector and position.y is an integer.

PlayerObj.transform.position = new Vector3(PlayerObj.transform.position.x,PlayerObj.transform.position.y += 10,PlayerObj.transform.position.z);

You should probably spend some time acquainting yourself with Unity’s documentation on GameObject scripting and Transforms.

I know, I am starting to learn Unity ^^
But I always get that damn error: Cannot midify a value type return value of “unityEngine.Transform.position”. Consider storing the value in a temporary variable.

In Unity, you can’t modify the individual x, y ,z or z values of the transform.position vector individually - you have to operate on it as a whole.

Get rid of that +=. You are already doing assignment on the whole position vector;

  PlayerObj.transform.position = new Vector3(PlayerObj.transform.position.x,PlayerObj.tranform.position.y + 10,PlayerObj.transform.position.z);

I think you can also do

PlayerObj.transform.position += new Vector3(0,10,0);

though I am not able to test it at this point.