Search Unity

Trying to create a Blink Ability, works fine until I use on Player GameObject

Discussion in 'Scripting' started by christopherbartlett, Feb 11, 2019.

  1. christopherbartlett

    christopherbartlett

    Joined:
    Feb 10, 2019
    Posts:
    2
    Hey everyone,

    I followed a tutorial on how to create Tracer's blink ability from Overwatch, where the player moves forward a certain distance while using a Raycast to detect collisions so they don't blink through walls. All was well when I tested it on a Cube, however as soon as I attached it to my player GameObject it didn't budge. While trying to figure out what the issue was I disabled the Character Controller component on the Player and I was able to blink properly however I couldn't move my character using the movement keys. Any help would be appreciated.

    Thanks.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. public class Blink : MonoBehaviour
    7. {
    8.     public int amount = 3;
    9.  
    10.     void Start()
    11.     {
    12.      
    13.         StartCoroutine("Reload");
    14.     }
    15.  
    16.     void Update()
    17.     {   if (amount != 0)
    18.         {  
    19.             if (Input.GetMouseButtonDown(1))
    20.             {
    21.  
    22.                 Vector3 ray = transform.TransformDirection(Vector3.forward);
    23.                 RaycastHit hit;
    24.                 if (Physics.Raycast(this.transform.position, ray, out hit, 10))
    25.                 {
    26.                     this.transform.position = hit.point -= this.transform.forward * 1;
    27.                 }
    28.                 else
    29.                 {
    30.                     this.transform.position += this.transform.forward * 10;
    31.                 }
    32.                 amount -= 1;
    33.             }
    34.         }
    35.     }
    36.     IEnumerator Reload()
    37.     {
    38.         yield return new WaitForSeconds(3);
    39.         if(amount < 3)
    40.         {
    41.             amount += 1;
    42.         }
    43.         StartCoroutine("Reload");
    44.     }
    45. }
    46.  
     
  2. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,913
    A CharacterController can trigger collisions sometimes, so maybe you are hitting that. Put a Debug.Log in if statement to see.

    Try putting the player on the Ignore Raycast layer.

    Or, use the Raycast overload that takes a LayerMask, and have a LayerMask variable to insert in there.
     
  3. christopherbartlett

    christopherbartlett

    Joined:
    Feb 10, 2019
    Posts:
    2
    I attached a script to check for collisions but since I'm not using a Rigidbody component nothing was detected. Putting the player on the Ignore Raycast layer made no difference. The player is able to blink occasionally but when it fails it jitters to the destination of the Raycast for a frame but fails to complete the blink.
     
  4. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,913
  5. TimmyTheTerrible

    TimmyTheTerrible

    Joined:
    Feb 18, 2017
    Posts:
    186
    Always use debug log to print to the console what object got hit by the ray, and maybe the distance to the object that was hit. Also, I would always use a LayerMask property so you can customize what your ray cast is affected by. Debug the daylights out of whatever your ray is intersecting! My guess is the ray is hitting something before getting to far so it looks like your not teleporting.