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. Dismiss Notice

Question Newb trying to control bullet direction

Discussion in 'Scripting' started by maD_Ghoul, May 17, 2023.

  1. maD_Ghoul

    maD_Ghoul

    Joined:
    Apr 10, 2022
    Posts:
    2
    Hey you-all!

    I'm delving into Unity and learning to code at the same time and have gotten myself into a very deep hole full of confusion. =]]

    I'm trying to make a small isometric ARPG game and decided to start with navigation and instantiating a sprite that will later on represent bullets/weapons/spells/etc. I need to be able to control the direction the sprite moves after being spawned and in the code below I've tried to make it so that it heads off in the direction the player sprite is facing to no avail.

    The player sprite itself does rotate almost as expected but somehow I cannot make the connection and tell Unity to send the spawned sprites the same way.

    Any help will be much appreciated!


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5.  
    6. public class playerMovement : MonoBehaviour
    7. {
    8.     [SerializeField] public float speed = 2f;
    9.     private Vector3 target;
    10.     [SerializeField] public GameObject bullet;
    11.     [SerializeField] Transform gun;
    12.     bool isAlive = true;
    13.     Rigidbody2D myRigidbody;
    14.     [SerializeField] private float rotationSpeed;
    15.    
    16.    
    17.     // Start is called before the first frame update
    18.     void Start()
    19.     {
    20.         target = transform.position;
    21.         myRigidbody = GetComponent<Rigidbody2D>();
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.         if(Input.GetMouseButtonDown(0))
    28.         {
    29.             //camera.main searches for "Main Camera" in game objects. If camera is different replace .main with .'cameraname'
    30.             target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    31.             target.z = transform.position.z;
    32.         }
    33.  
    34.         transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
    35.        
    36.         if (target != Vector3.zero)
    37.         {
    38.             Quaternion toRotation = Quaternion.LookRotation(Vector3.forward, target);
    39.             transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, rotationSpeed * Time.deltaTime);
    40.         }
    41.  
    42.     }
    43.  
    44.     void OnFire(InputValue value)
    45.     {
    46.         if (!isAlive)
    47.         {
    48.             return;
    49.         }
    50.         Instantiate(bullet, gun.position, transform.rotation);
    51.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Line 30 will ONLY work with an isometric camera. Otherwise your projected position won't be valid.

    Other than that particular first-glance issue, it may be time to start debugging!

    Here is how you can begin your exciting new debugging adventures:

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

    Once you understand what the problem is, you may begin to reason about a solution to the problem.

    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 names of the GameObjects or Components involved?
    - 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.

    Visit Google for how to see console output from builds. 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/

    If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

    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)" - Kurt Dekker (and many others)

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

    maD_Ghoul

    Joined:
    Apr 10, 2022
    Posts:
    2
    Thanks a lot for the awesome reply Kurt!

    Will have a sit and delve into the world of debugging as soon as I have an hour to spare!
     
  4. unUmGong

    unUmGong

    Joined:
    May 13, 2023
    Posts:
    11
    true_isometry.gif

    True Isometry would never employ a quaternion for the height reasons seen above.
    Quaternion is part of the old rotational system, and will be depreciated, legacy (without further support) in the near future. So i would recommend using the euler rotational rules instead. It should be the same as the logic about rotations that you would recieve in early math classes at school, while also its more suitable for an Isometric game.

    If you are just mimicking isometery using rotation in a 3D world you should mention this aswell in future posts.

    Good Luck!
     
  5. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,900
    I'm sorry what? Could you please elaborate what you're talking about? It seems to me that you're mixing some stuff up if you're talking about rotation in Unity (and mathematics) in general. So what will be deprecated exactly and what is the "old rotational system"? And then what is the new one?
     
  6. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    It's called trolling.
     
    Kurt-Dekker likes this.