Search Unity

warping player to locations?

Discussion in 'Scripting' started by techbinge, Dec 11, 2007.

  1. techbinge

    techbinge

    Joined:
    Jul 22, 2006
    Posts:
    88
    Is there a simple way to warp the player to a different location? Say something similar to moving the player to a checkpoint? I"m using the First Person Controller prefab. Maybe adjusting the translation of the prefab?

    Thanks in advance!
     
  2. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,290
    updating transform.position would probably work, but I'm not sure what exactly you want, probably some visual effect, too?
     
  3. techbinge

    techbinge

    Joined:
    Jul 22, 2006
    Posts:
    88
    That could work. If I move the parent object via translation will all the children move as well? I could always just attach an emitter to create a player spawned effect.

    What is the syntax for movement code? IE:

    Code (csharp):
    1.  
    2. playerobject.location.x = 5;
    3.  
    Thanks!
     
  4. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    playerobject.transform.position.x = 5.0;

    assuming playerobject is a game object. yes the kids should move with the parent...
     
  5. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    Here is a code that i made.

    This will have the player to be spawned to just over the ground (good if the terrain height varies) and it contains an particle effect :D

    http://pastecode.com/?show=de261d79

    I made this with no access to unity or any script writer program so it probobly contains errors. And there is things that i dont know how to write in javascript but i'm sure it can be done just need to find the right word (it is the loop).
    And i Don't know if the isGrounded thing works in this case.

    If you get the script to work would you may paste it back in the forum, plz.

    Good luck.
     
  6. techbinge

    techbinge

    Joined:
    Jul 22, 2006
    Posts:
    88
    Thanks for all the great responses. I came up w/ a simple, but elegant solution. Below is simplified version of the script I've used. As well, I've created custom meshes for our checkpoint / spawn point locations. These are placed in the map and our mappers then simply plug them into the script.

    I have to say, I've never been so impressed by a piece of software. Unity's data driven architecture is a dream and the fact that I was able to quickly script this (~5mins), have it integrated into the editor, and let my designers start working with it blows me away.

    Thanks again everyone!

    NOTE: I'm sure this could be a bit cleaner, but for now its working great and I've got other issues to tend to :).

    Code (csharp):
    1.  
    2. var PlayerController : GameObject; // This is the physical object our player is parented by
    3. var SpawnPoint1 : GameObject; // Our first checkpoint / spawn point
    4. var SpawnPoint2 : GameObject; // etc.
    5. var SpawnPoint3 : GameObject; // etc.
    6.  
    7. function Update () {
    8.     if (Input.GetKeyDown ("1")){
    9.  
    10.         // set our players position to our spawn point upon event (in this case a keypress)
    11.         PlayerController.transform.position = SpawnPoint1.transform.position;
    12.         }
    13.     if (Input.GetKeyDown ("2")){
    14.  
    15.         PlayerController.transform.position = SpawnPoint2.transform.position;
    16.         }
    17.     if (Input.GetKeyDown ("3")){
    18.  
    19.         PlayerController.transform.position = SpawnPoint3.transform.position;
    20.         }      
    21. }
     
  7. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    Nice code, hope it works.
     
  8. techbinge

    techbinge

    Joined:
    Jul 22, 2006
    Posts:
    88
    One thing I'm now noticing is that if I press 1 it warps me correctly. If I move a bit, then press 1 again, it does nothing. Not sure if this is because this is all being done in the Update function, or what?

    Any ideas?
     
  9. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You almost always want input code to be in Update. The script you posted is fine, so I'd have to guess there's something else in the non-simplified version that's causing the problem.

    BTW, it's usually more convenient to refer directly to the component that you're accessing. For example,

    Code (csharp):
    1. var PlayerController : Transform; // This is the physical object our player is parented by
    2. var SpawnPoint1 : Transform; // Our first checkpoint / spawn point
    3. [...]
    4.       PlayerController.position = SpawnPoint1.position;
    Less typing, and a bit faster. :)

    --Eric