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

How to make a BASIC gattling gun script for an airplane in 3D Core?

Discussion in 'Scripting' started by CMAN12345, Nov 11, 2022.

  1. CMAN12345

    CMAN12345

    Joined:
    Nov 3, 2022
    Posts:
    3
    I have been trying to make a basic script for a fighter jet and i have no clue where to start. and all of the forums i have seen are too complicated to understand to my beginer mind. can some one pls hep me accomplish the following goals

    the first goal I am struggling with is getting the gun to fire when i press space from right in front of the plane in the direction it is facing(X, Y, Z Axis) and deal around 20 units damage


    the second is to create a basic UI showing the crosshair, a sign that says "Locked on" showing the target it is locked on to, Hit points, and a sign that says "Hit" in green when dealing damage, and in red when the player gets a kill.

    the third is to lock(KEY is S***ft) on to a target after being near the crosshair for .5 seconds while 200- meters away

    the fourth is to deploy a missile(by pressing ENTER) after locking on to a target and can be used only every 4 secends

    the fifth is to deploy flares(KEY is F) that can only be used every 50 seconds to divert missiles







    the projectile for the gattling gun is named "Bullet"
    the projectile for the missile is named "Missile"


    below is my only code I know how to create


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class PlayerController : MonoBehaviour
    {
    public float speed;
    public float rotationSpeed;
    public float verticalInput;
    public float horizontalInput;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void FixedUpdate()
    {
    // get the user's vertical input
    verticalInput = Input.Get("Vertical");

    // move the plane forward at a constant rate
    transform.Translate(Vector3.forward * speed * Time.deltaTime);

    // tilt the plane up/down based on up/down arrow keys
    transform.Rotate(Vector3.left * rotationSpeed * Time.deltaTime * verticalInput);

    //tilt the plane on the z axis based on left & right arrow keys
    transform.Rotate(Vector3.forward * rotationSpeed * Time.deltaTime * verticalInput);
    }


    }
     
  2. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    9,799
    If that's the only code you know how to create, you need to take a step back and start from a more beginner standpoint. All the examples you've seen only seem complicated because you're just getting started, but they're likely very simple once you've grown more accustomed to development.

    Start from an earlier point, like here.

    https://learn.unity.com/project/unit-1-driving-simulation
     
    Kurt-Dekker and angrypenguin like this.
  3. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,519
    First up, this should go in the Scripting forum, not in General Discussion, and the first thing you're likely to be told there is to put your code in code tags. This sounds nitpicky, but it helps to keep things organised, easily readable, and generally less messy than they'd otherwise be.

    Second, you're going in the right direction by breaking things down into small steps, but they're not small enough yet. For example, this...
    ...is at least three steps, probably more.
    1. Respond to the space key being pressed by calling a method.
    2. Have a method which spawns a projectile in the correct position.
    3. When that projectile hits something, deal 20 damage.

    This implies a few extra steps which, based on your question, I would assume aren't being covered elsewhere yet.
    - What is "damage" and how do you deal it? You will need to create something which represents this, perhaps by making a "Health" component.
    - What does a projectile do? You will need to define this, probably by making a prefab.

    Your other points need to be similarly broken down. What is "locked on" and how is that represented? Until you know that you can't show it in your UI. And so on and so forth.

    This process of breaking multi-part problems down into individual steps is fundamental to programming. Computers are fast but stupid. So we have to tell them how to solve problems in tiny steps - copy a value, compare one thing to another, evaluate this calculation, call this method. Programming is literally the process of describing those steps to a computer, doing that effectively takes a bit of practice in breaking up broad goals ("getting the gun to fire") into tiny steps ("make a copy of the Bullet prefab, put it in this position...").

    So, for some concrete help:
    1. Do you have a prefab, or something, to define your bullets?
    2. Do you know how to spawn them in a position?
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    Please do not use the General Discussion for support. The forum description states this.

    I'll move your post to the Scripting forum. I always ask and it's nearly always ignored but please edit your post and use code-tags in the future when posting code.

    Thanks.
     
  5. CMAN12345

    CMAN12345

    Joined:
    Nov 3, 2022
    Posts:
    3

    1 yes i do have a prefab for them
    2 i am unsure how to get the bullet in a specified position
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    Instantiate() has an overload that accepts a position argument.