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

Problem with Collision

Discussion in 'Scripting' started by Milambur88, Jun 1, 2020.

  1. Milambur88

    Milambur88

    Joined:
    Jan 15, 2020
    Posts:
    28
    I am having a problem where when my game window size changes it seems to stop my collision detection from my sword swing.

    so any time i adjust the resolution the game is being displayed in i have to spend time trying to find the correct place to put my collision detection.

    is there a better way of doing this as i currently just have an empty object assigned to my sword and then adjust the x,y of the empty object.

    below is my code.

    Code (CSharp):
    1. public class InflictDamage : MonoBehaviour
    2. {
    3.     public int DamageDealt = 5;
    4.     public float TargetDistance;
    5.     public float AllowedRange = 2.7f;
    6.  
    7.     // Update is called once per frame
    8.     void Update()
    9.     {
    10.         if (Input.GetButtonDown("Fire1"))
    11.         {
    12.             RaycastHit hit;
    13.             if (Physics.Raycast(transform.position, transform.TransformDirection (Vector3.forward), out hit))
    14.             {
    15.                 TargetDistance = hit.distance;
    16.                 if (TargetDistance <= AllowedRange)
    17.                 {
    18.                     hit.transform.SendMessage("DeductPoints", DamageDealt, SendMessageOptions.DontRequireReceiver);
    19.                 }
    20.             }
    21.         }
    22.     }
    23.  
    24. }
     
    Last edited: Jun 1, 2020
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    Are you certain it's the raycast that's being affected? Have you checked to see that it's detecting your GetButtonDown? Seems far more likely that changing the window would affect the input (since input by default requires the window be focused, maybe it's losing focus or there's a bug with window focus or something), and seems near-impossible that the window resizing would affect anything in the physics engine.

    Can you show this code?
     
    Milambur88 likes this.
  3. Milambur88

    Milambur88

    Joined:
    Jan 15, 2020
    Posts:
    28
    so i don't use any code I just move the empty box with the above code attached to this and the empty object is a child of my sword.

    Edit. Also i found the error for my collision changing was i didn't save the scene.
     
    Last edited: Jun 2, 2020