Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Teleport my Character

Discussion in 'Scripting' started by TechnoObi, Apr 25, 2014.

  1. TechnoObi

    TechnoObi

    Joined:
    Nov 30, 2013
    Posts:
    82
    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?
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    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).
     
  3. Tiki

    Tiki

    Joined:
    Mar 3, 2013
    Posts:
    299
    Assign the player a "player" tag.

    In C# -

    Code (csharp):
    1.  
    2.  
    3. GameObject PlayerObj = GameObject.FindWithTag("Player");
    4.  
    5. If Conditions are met, etc. {
    6.     PlayerObj.transform.position = new Vector3(PlayerObj.x,PlayerObj.y += 10,PlayerObj.z);
    7. }
    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.
     
    Last edited: Apr 25, 2014
  4. TechnoObi

    TechnoObi

    Joined:
    Nov 30, 2013
    Posts:
    82
    Thx, I have now this code:

    Code (csharp):
    1. if(oberwelt) {
    2.                 PlayerObj.transform.position = new Vector3(PlayerObj,PlayerObj += 10,PlayerObj);
    3.        
    4.             }
    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?
     
  5. Tiki

    Tiki

    Joined:
    Mar 3, 2013
    Posts:
    299
    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 :p

    BTW, if (upper world?) lol
     
    Last edited: Apr 25, 2014
  6. TechnoObi

    TechnoObi

    Joined:
    Nov 30, 2013
    Posts:
    82
    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
    Code (csharp):
    1. if(oberwelt) {
    2.                 PlayerObj.transform.position.y += 10;
    3.        
    4.             }
    i get this error: Cannot modify a vlue type return value of UnityEngine.Transform.position. Consider storing the value in a temporary variable.
     
  7. Tiki

    Tiki

    Joined:
    Mar 3, 2013
    Posts:
    299
    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.
     
  8. TechnoObi

    TechnoObi

    Joined:
    Nov 30, 2013
    Posts:
    82
    I am also not really smart at the moment.

    What should I do know? :)
     
  9. Tiki

    Tiki

    Joined:
    Mar 3, 2013
    Posts:
    299
    Just change the new Vector3() contents to be PlayerObj.transform.position.x .y .z
     
  10. TechnoObi

    TechnoObi

    Joined:
    Nov 30, 2013
    Posts:
    82
    Do you mean:
    Code (csharp):
    1.  if(oberwelt) {
    2.                 PlayerObj.transform.position = PlayerObj.transform.position.y += 10;
    3.        
    4.             }
     
  11. Tiki

    Tiki

    Joined:
    Mar 3, 2013
    Posts:
    299
    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.
     
  12. TechnoObi

    TechnoObi

    Joined:
    Nov 30, 2013
    Posts:
    82
    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.
     
  13. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    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;


    Code (csharp):
    1.   PlayerObj.transform.position = new Vector3(PlayerObj.transform.position.x,PlayerObj.tranform.position.y + 10,PlayerObj.transform.position.z);
    I think you can also do

    Code (csharp):
    1. PlayerObj.transform.position += new Vector3(0,10,0);
    though I am not able to test it at this point.