Search Unity

Increase raycast range

Discussion in 'Scripting' started by ThisIsSparta, Jul 25, 2016.

  1. ThisIsSparta

    ThisIsSparta

    Joined:
    May 4, 2014
    Posts:
    142
    Hi all ;)

    I found this script on line and I was wondering how can I increse the raycast. any help would be appreciated! thank you very much!

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Player : MonoBehaviour {
    5.  
    6.     public bool drawDebugGizmos = false;
    7.     public float timeToMove = 1f;
    8.  
    9.     Vector3 invalidPosition = new Vector3(9999, 9999, 9999);
    10.     Vector3 positionToMoveTo;
    11.     Vector3 currentMove;
    12.     Vector3 checkDirection;
    13.     bool checkedDiagonal = false;
    14.     bool isMoving = false;
    15.     Vector3 transformUp;
    16.     Vector3 currentMoveCenter;
    17.     Ray currentMoveRay;
    18.     Ray wallCheckRay;
    19.  
    20.     void Start() {
    21.         positionToMoveTo = invalidPosition;
    22.     }
    23.  
    24.     void Update () {
    25.         if (isMoving) {
    26.             return;
    27.         }
    28.  
    29.         if (Input.GetKeyDown(KeyCode.W)) {
    30.             ResetChecks();
    31.             currentMove = transform.forward;
    32.             Move(currentMove);
    33.             if (positionToMoveTo != invalidPosition) {
    34.                 StartCoroutine("PerformMove");
    35.             }
    36.         }
    37.         else if (Input.GetKeyDown(KeyCode.A)) {
    38.             ResetChecks();
    39.             currentMove = transform.right * -1;
    40.             Move(currentMove);
    41.             if (positionToMoveTo != invalidPosition) {
    42.                 StartCoroutine("PerformMove");
    43.             }
    44.         }
    45.         else if (Input.GetKeyDown(KeyCode.S)) {
    46.             ResetChecks();
    47.             currentMove = transform.forward * -1;
    48.             Move(currentMove);
    49.             if (positionToMoveTo != invalidPosition) {
    50.                 StartCoroutine("PerformMove");
    51.             }
    52.         }
    53.         else if (Input.GetKeyDown(KeyCode.D)) {
    54.             ResetChecks();
    55.             currentMove = transform.right;
    56.             Move(currentMove);
    57.             if (positionToMoveTo != invalidPosition) {
    58.                 StartCoroutine("PerformMove");
    59.             }
    60.         }
    61.     }
    62.  
    63.     IEnumerator PerformMove() {
    64.         isMoving = true;
    65.         float elapsedTime = 0f;
    66.         Vector3 initialPosition = transform.position;
    67.         Vector3 inititalUp = transform.up;
    68.  
    69.         while (elapsedTime < timeToMove) {
    70.             transform.position = Vector3.Lerp(initialPosition, positionToMoveTo, (elapsedTime / timeToMove));
    71.             transform.up = Vector3.Lerp(inititalUp, transformUp, (elapsedTime / timeToMove));
    72.             elapsedTime += Time.deltaTime;
    73.             yield return null;
    74.         }
    75.  
    76.         transform.position = positionToMoveTo;
    77.         transform.up = transformUp;
    78.  
    79.         positionToMoveTo = invalidPosition;
    80.         isMoving = false;
    81.     }
    82.  
    83.     void Move(Vector3 offset) {
    84.         Vector3 pointToCheck = transform.position + offset;
    85.  
    86.         currentMoveCenter = pointToCheck;
    87.         currentMoveRay = new Ray(currentMoveCenter, checkDirection);
    88.  
    89.         RaycastHit hit;
    90.         if (Physics.Raycast(currentMoveRay, out hit, 1f)) {
    91.             if (hit.collider.tag == "Walkable") {
    92.                 transformUp = hit.normal;
    93.                 positionToMoveTo = pointToCheck;
    94.             }
    95.         }
    96.  
    97.         if (positionToMoveTo != invalidPosition) {
    98.             return;
    99.         }
    100.  
    101.         if (WallCheck()) {
    102.             StartCoroutine("ChangeOrientation");
    103.             return;
    104.         }
    105.         else if (!checkedDiagonal) {
    106.             checkedDiagonal = true;
    107.             checkDirection = currentMove * -1;
    108.             Move(currentMove + transform.up * -1);
    109.         }
    110.     }
    111.  
    112.     IEnumerator ChangeOrientation() {
    113.         isMoving = true;
    114.         float elapsedTime = 0f;
    115.         Vector3 inititalUp = transform.up;
    116.  
    117.         while (elapsedTime < timeToMove) {
    118.             transform.up = Vector3.Lerp(inititalUp, transformUp, (elapsedTime / timeToMove));
    119.             elapsedTime += Time.deltaTime;
    120.             yield return null;
    121.         }
    122.  
    123.         transform.up = transformUp;
    124.  
    125.         isMoving = false;
    126.     }
    127.  
    128.     void ResetChecks() {
    129.         checkDirection = -1 * transform.up;
    130.         checkedDiagonal = false;
    131.     }
    132.  
    133.     bool WallCheck() {
    134.         wallCheckRay = new Ray(transform.position, currentMove);
    135.         RaycastHit hit;
    136.         if (Physics.Raycast(wallCheckRay, out hit, 1f)) {
    137.             if (hit.collider.tag == "Walkable") {
    138.                 transformUp = hit.normal;
    139.                 return true;
    140.             }
    141.         }
    142.  
    143.         return false;
    144.     }
    145.  
    146.     void OnDrawGizmos() {
    147.         if (!drawDebugGizmos) {
    148.             return;
    149.         }
    150.  
    151.         // Draw a gizmo at the center point for our current move. This is where
    152.         // we raycast from etc.
    153.         Gizmos.color = Color.yellow;
    154.         Gizmos.DrawWireSphere(currentMoveCenter, 0.1f);
    155.  
    156.         // Draw the ray we use to check if there's a walkable node beneath our feet.
    157.         Gizmos.color = Color.red;
    158.         Gizmos.DrawRay(currentMoveRay);
    159.  
    160.         // Draw the ray we use to check if there is a wall in the direction we're
    161.         // trying to move.
    162.         Gizmos.color = Color.blue;
    163.         Gizmos.DrawRay(wallCheckRay);
    164.     }
    165. }
    166.  
     
  2. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    if(Physics.Raycast(currentMoveRay, outhit, 1f))
    Just increase the 1f
    Check the scripting documentation on raycast, everything you need to know is in there.
    If you want to see the ray, use Debug.DrawRay(currentMoveRay.origin, currentMoveRay.direction * 10f) or however long you want it to be.
     
  3. ThisIsSparta

    ThisIsSparta

    Joined:
    May 4, 2014
    Posts:
    142
    Hey man. The debug is stil in the script. The point is I tried to increase that but nothing changes.
     
  4. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    Ah hehe didn't scroll all the way down.
    Make some rayLength variables for all your rays so you can test it while running h the code.
    And just check the docs then see all possible manners for a ray and at which place length is set
     
  5. ThisIsSparta

    ThisIsSparta

    Joined:
    May 4, 2014
    Posts:
    142
  6. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
  7. ThisIsSparta

    ThisIsSparta

    Joined:
    May 4, 2014
    Posts:
    142
    Omg I'm reading everything about it but still can't find an answer...seems like a problem so simple to solve -,- plz I'm going mad
     
  8. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you've already been given the answer to increasing the distance the raycast uses
     
  9. ThisIsSparta

    ThisIsSparta

    Joined:
    May 4, 2014
    Posts:
    142
    of course is not working...why should I continue posting if it was working?

    If I change to zero i don't have raycasts anymore, but if I set it to 10 for example I have the same result of setting it to 1
     
  10. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    The third parameter of a raycast (given that shape, lots of shapes for the raycast function...) is the distance the raycast is to check. This is very much a "Q: whats 1+1" "A: 2" thing...

    If it's "not working" you're either asking the wrong question (bmb and jister hedged a little on this by mentioning the changes to Debug.DrawRay and Gizmo.DrawRay in case it's "how can I show the ray as being longer", displaying a ray and raycasting work differently and are totally separate etc.), or something is not setup correctly elsewhere (missing collider so a raycast of any length is never going to hit whatever it is etc.).

    Given that this is code you've found online and you've not really explained what it is you are trying to do, it might not even be code that is applicable to your purposes o_O:p
    Show us your altered code (maybe you altered it incorrectly, we can't know that unless you show it), explain more specifically how it's not working and what you want it to do etc.

     
  11. ThisIsSparta

    ThisIsSparta

    Joined:
    May 4, 2014
    Posts:
    142
    gimme 2 sec and I'll post the project.
     
  12. ThisIsSparta

    ThisIsSparta

    Joined:
    May 4, 2014
    Posts:
    142
    You can find here the project. two scripts are missings from the levelcontroller object but they don't affects the raycast problem...all is working fine in the project but if you want to increase the space between the green cubes ( and this means you have to increase the raycast) you'll have difficulties...let me know plz! and thx for the interest!
     

    Attached Files: