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

Create a teleportation spell

Discussion in 'Scripting' started by BDamien54, Mar 10, 2015.

  1. BDamien54

    BDamien54

    Joined:
    Feb 22, 2015
    Posts:
    12
    Hi,

    I'm currently creating a First Person Game and I'm trying to create a teleportation system that looks like this :
    .
    I don't know how to do it ... I think I've got to set a ray from the player of a definied lenght and get a transform from the end of the ray and apply it to the player when the player press a defined button.The problem is don't really know how to use the rays and I'm not sure of the script I've to make..
    Can you help me please ?

    I'm sorry if my english isn't perfect ; I'm french.

    Thank's in advance ! :)

    PS : If anyone knows how I can create the acceleration effect when the player teleports himself I would be glad to know how I can do it too ! :D
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    Hi,

    Split the work into two steps:

    1. Target a point on the map.

    2. Teleport to a target point.

    If you split them into separate scripts, each script will be shorter and easier to write. You can also use the targeting script (#1) for other purposes in the future, too.

    For #1, create or activate a targeting GameObject. This GameObject could be a particle effect or mesh without a collider. When targeting, run a raycast in Update(). Move the targeting GameObject to the raycast's hit position.

    For #2, consider using a coroutine. Temporarily disable the player's control. In the coroutine, increase the field of view angle over, say, 1 second. This will make the view look warped. Also move the player manually toward the target in the coroutine. In the last 1 second of the coroutine, gradually bring the field of view back to its normal setting. At the end of the coroutine, re-enable the player's control.
     
    BDamien54 likes this.
  3. BDamien54

    BDamien54

    Joined:
    Feb 22, 2015
    Posts:
    12
    Thanks a lot for your answer ! Can you just explain me how to use the raycast for #1 please? I don't know how it works ...

    Thanks in advance
     
  4. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    BDamien54 and Schneider21 like this.
  5. BDamien54

    BDamien54

    Joined:
    Feb 22, 2015
    Posts:
    12
    Thanks you very much !! :)
     
  6. Kooth

    Kooth

    Joined:
    Feb 23, 2015
    Posts:
    53
    Salut !
    I'm french sorry for my english .


    Code (JavaScript):
    1.  
    2. var hit: RaycastHit;
    3. var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    4.  
    5.  
    6. if (Physics.Raycast(ray, hit)) {
    7.  
    8. if(Input.GetKeyDown(KeyCode.E)) {
    9. transform.position = hit.point
    10.  
    11. }
    12.  
    13.  
    14. }
    15.  
    16.  
    17.  
    18.  
    I don't try but maybe can work .
     
    Last edited: Mar 13, 2015
    BDamien54 likes this.
  7. BDamien54

    BDamien54

    Joined:
    Feb 22, 2015
    Posts:
    12
    Merci à toi ! :)
     
  8. jmdoidao

    jmdoidao

    Joined:
    Jul 30, 2012
    Posts:
    1
    well played Kooth, that was a nice trolling hahahhaha...
    @BDamien54 , just follow the link that TonyLi posted
     
    Kooth and BDamien54 like this.
  9. BDamien54

    BDamien54

    Joined:
    Feb 22, 2015
    Posts:
    12
    Hi,

    Thank's you again, with your help, I succeeded to move the targeting GameObject to the raycast's hit position.
    I'm currently creating the coroutine but I don't know how to increase
    progressively the camera's field of view for a determined time (let's say 1 second). Can you help me please ? The only documentation I found is here : http://docs.unity3d.com/ScriptReference/Camera-fieldOfView.html.

    I also searched how to disable the player's control, do you think this code could be effective ?

    Code (CSharp):
    1. CharacterController cc =GetComponent(typeof(CharacterController))asCharacterController;
    2. cc.enabled =false;

    Thank's in advance ! :)

    Again, sorry if my english isn't perfect ... :/

    Have a good day ! ;D
     
  10. BDamien54

    BDamien54

    Joined:
    Feb 22, 2015
    Posts:
    12
    Hi,

    That's what I did ! ;) Even if he tried to troll me, he actually gave me some documentation which can always help ! :p
     
  11. Kooth

    Kooth

    Joined:
    Feb 23, 2015
    Posts:
    53
    It was not a troll , just a bug :p

    (Tu as essayé ce le code ? je crois qu'il marche , rajoute peut-être une condition d'appui sur un bouton)
     
    BDamien54 likes this.
  12. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    This is off the top of my head. I just typed it in here. I'm not near Unity right now, so it's not tested.
    Code (csharp):
    1. public float totalDuration = 1; // Duration for entire teleport sequence.
    2. public float warpFovDuration = 0.25f; // Duration to warp into/out of wide FOV.
    3. public float warpFov = 180; // Field of view while teleporting.
    4.  
    5. IEnumerator Teleport(Vector3 destination) {
    6.     // Disable player controls:
    7.     // (Specify your player input script here)
    8.     var playerControl = GetComponent<FirstPersonController>();
    9.     playerControl.enabled = false;
    10.  
    11.     // Record initial state:
    12.     var initialTime = Time.time;
    13.     var initialPosition = transform.position;
    14.     var initialFov = Camera.main.fieldOfView;
    15.  
    16.     // Teleport a little bit each frame:
    17.     while (Time.time < startTime + duration) {
    18.         var elapsed = Time.time - startTime;
    19.         transform.position = Vector3.Lerp(initialPosition, destination, elapsed / totalDuration);
    20.         if (Time.time < startTime + warpFovDuration) {
    21.             // Ease FOV into teleport mode:
    22.             Camera.main.fieldOfView = Mathf.Lerp(initialFov, warpFov, elapsed / warpFovDuration);
    23.        else if (Time.time > startTime + duration - warpFovDuration) {
    24.             // Ease FOV out of teleport mode:
    25.             var easeOutElapsed = elapsed - (duration - warpFovDuration);
    26.             Camera.main.fieldOfView = Mathf.Lerp(warpFov, initialFov, easeOutElapsed / warpFovDuration);
    27.         }
    28.         yield return null;
    29.     }
    30.  
    31.     // Enable player controls:
    32.     playerControl.enabled = true;
    33. }
     
    BDamien54 likes this.
  13. BDamien54

    BDamien54

    Joined:
    Feb 22, 2015
    Posts:
    12
    Ah d'accord ! :p Je vais regarder ça, merci de ton aide en tout cas !
     
    Kooth likes this.
  14. BDamien54

    BDamien54

    Joined:
    Feb 22, 2015
    Posts:
    12
    Thank's you, I will try this ! :D
    It gives me a good idea of what I've to do ! ;)
    Thank's you again !! :p
     
  15. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    Happy to help! :)