Search Unity

  1. Unity 2019.1 beta is now available.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We're looking for insight from anyone who has experience with game testing to help us better Unity. Take our survey here. If chosen to participate you'll be entered into a sweepstake to win an Amazon gift card.
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Unity 2018.3 is now released.
    Dismiss Notice
  6. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

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


    Feb 10, 2019
    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.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    6. public class Blink : MonoBehaviour
    7. {
    8.     public int amount = 3;
    10.     void Start()
    11.     {
    13.         StartCoroutine("Reload");
    14.     }
    16.     void Update()
    17.     {   if (amount != 0)
    18.         {  
    19.             if (Input.GetMouseButtonDown(1))
    20.             {
    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. }
  2. Stardog


    Jun 28, 2010
    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


    Feb 10, 2019
    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


    Jun 28, 2010
  5. TimmyTheTerrible


    Feb 18, 2017
    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.