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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Bug Directonal Collisions

Discussion in 'Scripting' started by Redstoneperson, Jun 17, 2020.

  1. Redstoneperson

    Redstoneperson

    Joined:
    Aug 26, 2019
    Posts:
    11
    Heyo! I have been trying to make my own version of Beat Saber in unity, designed for preformance, however, in Update() it won't get past the first if statement when I try to slice a cube. It is supposed to Destroy the cube.
    I put the script on my hand anchors, and the saber is inside of the hand anchors. I tried putting the script on the saber, I tried everything, I searched on google. I tried getting help on a discord server, no help.

    Heres the Update Method:
    Code (CSharp):
    1. void Update()
    2.     {
    3.         RaycastHit hit;
    4.         if (Physics.Raycast(transform.position, transform.forward, out hit, 1, layer))
    5.         {
    6.             UnityEngine.Debug.Log("Passed first if statement");
    7.             GameObject g = hit.transform.gameObject;
    8.             if (Vector3.Angle(transform.position-previousPos, hit.transform.up) > 130)
    9.             {
    10.                 UnityEngine.Debug.Log("Reached");
    11.                 UnityEngine.Object.Destroy(g);
    12.                 UnityEngine.Debug.Log("Destroyed");
    13.             }
    14.         }
    15.         previousPos = transform.position;
    16.     }
    Please help!
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    You have some log statements in there. Which ones are printing and which are not?
     
  3. Redstoneperson

    Redstoneperson

    Joined:
    Aug 26, 2019
    Posts:
    11
    none
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Have you tried visualizing the ray that you are raycasting in OnDrawGizmos so you're sure your ray origin and direction are correct? That would be the next step

    After that, if the rays look correct, I'd double check the layermask you're using in Raycast.
     
  5. Redstoneperson

    Redstoneperson

    Joined:
    Aug 26, 2019
    Posts:
    11
    I didn't understand a word you just said, this is my first time EVER writing any script in C# other than a web browser
     
  6. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    10,005
    Put a Debug.DrawRay above your first if. Something like this:
    Code (CSharp):
    1. Debug.DrawRay(transform.position, transform.forward, Color.red);
    And observe if the line pointing to the right direction. and if it is long enough (1 unit)
     
  7. nsxdavid

    nsxdavid

    Joined:
    Apr 6, 2009
    Posts:
    476
    You are only casting like 1 unit away. And no telling what layer you are masking for or what layer your target is on from that example.