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

Question When i move foward my gun shoots downwards? How do i fix

Discussion in 'Scripting' started by Makoopay, Oct 3, 2022.

  1. Makoopay

    Makoopay

    Joined:
    Jan 11, 2021
    Posts:
    3
    When i move foward using the W key my gun shoots fowards but when i move diagonally using WA or WD
    it shoots correctly

    Heres my code for my gun:

    Code (CSharp):
    1. public class GunScript : MonoBehaviour
    2. {
    3.     //Properties
    4.     public float range = 100f;
    5.     public GameObject impacteffect;
    6.     public ParticleSystem muzzleflash;
    7.     public bool IsShooting = false;
    8.     public GameObject Hand;
    9.     public Shaker camShaker;
    10.     public ShakePreset ShakerPreset;
    11.     public AudioSource audioSource;
    12.     public AudioClip clip;
    13.     public float volume = 0.5f;
    14.     public LineRenderer lr;
    15.     public float linedrawSpeed = 2f;
    16.  
    17.     //Camera
    18.     public Camera fpsCam;
    19.  
    20.     //Animators
    21.     public Animator animator;
    22.     public Animator handAnimator;
    23.  
    24.     //Raycast
    25.     RaycastHit hit;
    26.  
    27.     void Start()
    28.     {
    29.         //AnimatorComponents
    30.         lr = GetComponent<LineRenderer>();
    31.         animator = GetComponent<Animator>();
    32.         handAnimator = Hand.GetComponent<Animator>();
    33.     }
    34.  
    35.     // Update is called once per frame
    36.     void Update()
    37.     {
    38.         if (Input.GetButtonDown("Fire1"))
    39.         {
    40.             //Shoot Function
    41.             Shoot();
    42.  
    43.         }
    44.     }
    45.  
    46.  
    47.     void Shoot()
    48.     {
    49.         //Raycast from camera
    50.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    51.  
    52.  
    53.         if (Physics.Raycast(ray, out hit))
    54.         {
    55.  
    56.             //Get what bullet hits
    57.             Debug.Log(hit.transform.name);
    58.             Debug.Log(hit.transform.name);
    59.  
    60.  
    61.  
    62.             //if player is range/melee attack
    63.             if (IsShooting == true)
    64.                 {
    65.                 //Does nothing and returns to code
    66.                     return;
    67.                 }
    68.                 if (IsShooting == false)
    69.                 {
    70.                 //Play SoundEffect
    71.                 audioSource.PlayOneShot(clip, volume);
    72.                 //Particles
    73.                 GameObject impactGO = Instantiate(impacteffect, hit.point, Quaternion.LookRotation(hit.normal));
    74.                 muzzleflash.Play();
    75.                 //Destroy Particles
    76.                 Destroy(impactGO, 2f);
    77.                 //CameraShake
    78.                 camShaker.Shake(ShakerPreset);
    79.                 //Check if what raycast hit has rigidbody
    80.                 if (hit.rigidbody != null)
    81.                     {
    82.                     //Check if what raycast hit has Enemy tag
    83.                     if (hit.transform.gameObject.tag == "Enemy")
    84.                     {
    85.                         //Sets enemy tag to dead enemy
    86.                         hit.transform.gameObject.tag = "Dead Enemy";
    87.                     }
    88.  
    89.                     //Pushes Enemy
    90.                         hit.rigidbody.AddForce(ray.direction * 500f);
    91.                     }
    92.  
    93.                 //Starts the GunCycle so the gun dosent keep shooting nonstop
    94.                 StartCoroutine(GunCycle());
    95.             }
    96.  
    97.         }
    98.  
    99.     }
    100.  
    101.  
    102.  
    103.  
    104.     IEnumerator GunCycle ()
    105.     {
    106.         //Tells the code that the player is shooting
    107.         IsShooting = true;
    108.         Debug.Log("Player Is Attacking");
    109.         animator.SetBool("Shooting", true);
    110.         handAnimator.SetBool("Shooting", true);
    111.         //Line Renderer
    112.  
    113.         lr.enabled = true;
    114.         lr.SetPosition(0, hit.point);
    115.         lr.SetPosition(1, muzzleflash.transform.position);
    116.         float t = 0;
    117.         float time = 1;
    118.         Vector3 orig = lr.GetPosition(1);
    119.         Vector3 orig2 = lr.GetPosition(0);
    120.         lr.SetPosition(1, orig2);
    121.         Vector3 newpos;
    122.         for (; t < time; t += Time.deltaTime * 15)
    123.         {
    124.             newpos = Vector3.Lerp(orig, orig2, t / time);
    125.             lr.SetPosition(1, newpos);
    126.             yield return null;
    127.         }
    128.         lr.SetPosition(1, orig2);
    129.  
    130.  
    131.         //Cooldown so animation could play
    132.         yield return new WaitForSeconds(0.3f);
    133.  
    134.         //Tells the code that the player is not shooting
    135.         lr.enabled = false;
    136.         IsShooting = false;
    137.         animator.SetBool("Shooting", false);
    138.         handAnimator.SetBool("Shooting", false);
    139.         Debug.Log("Player Is Not Attacking");
    140.      
    141.     }
    142.     IEnumerator LineDraw()
    143.     {
    144.         float t = 1;
    145.         float time = 0;
    146.         Vector3 orig = lr.GetPosition(1);
    147.         Vector3 orig2 = lr.GetPosition(0);
    148.         lr.SetPosition(0, orig);
    149.         Vector3 newpos;
    150.         for (; t > time; t += Time.deltaTime)
    151.         {
    152.             newpos = Vector3.Lerp(orig2, orig, t / time);
    153.             lr.SetPosition(0, newpos);
    154.             yield return null;
    155.         }
    156.         lr.SetPosition(0, orig2);
    157.     }
    158.  
    159. }
    160.  
    Anyone have an idea why this is happening?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,970
    Nope, but here's how you can get to the bottom of it.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling
    Debug.Log()
    statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    When in doubt, print it out!(tm)

    Note: the
    print()
    function is an alias for Debug.Log() provided by the MonoBehaviour class.

    You must find a way to get the information you need in order to reason about what the problem is.
     
  3. Makoopay

    Makoopay

    Joined:
    Jan 11, 2021
    Posts:
    3
    did this and still couldnt solve the problem
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,970
    Oh cool, show us what you output and how those values did not reveal your problem.

    At a minimum I would expect that you outputted the directional vector you THINK the shot should go in, and the vector that it ACTUALLY went in.
     
  5. Makoopay

    Makoopay

    Joined:
    Jan 11, 2021
    Posts:
    3
    i think i fixed the bug, but it works on some computers and dont in some.