How do I assign shooting to a specific key?

Viewed 38

I'm very new to coding and Unity. I want to make it so that my character shoots a shuriken prefab when the S key is pressed down.

The issue is that I cannot get the shuriken to appear when I press a key. When I just have the ShurikenLeft and ShurikenRight prefabs in the hierarchy, they will still shoot in the right direction and disappear after 3 seconds which is what I want. I just can't map it to a specific key so that it appears when I press it down.

I used this guide to make this: https://www.youtube.com/watch?v=1QOsUrXWMWY The guy says that the shoot button is assigned to CTRL by default but it still doesn't work when I click that. I've gone to Edit > Project settings > Input and changed Fire1 to Throw1 so that also isn't the problem.

Here is all of my code:

ShurikenScript:

        public class ShurikenScript : MonoBehaviour {

        public float velX = 10f;
        float velY = 0f;
        Rigidbody2D rb;
        // Use this for initialization
        void Start () {
            rb = GetComponent<Rigidbody2D>();
        }
    
        // Update is called once per frame
        void Update () {
            rb.velocity = new Vector2(velX, velY);
            Destroy(gameObject, 3f);
        }
    }

PlayerMovement:

    using System.Collections;
    using System.Collections.Generic;
    using System.Security.Cryptography;
    using System.Threading;
    using UnityEngine;

    public class PlayerMovement : MonoBehaviour {

        float velX;
        float velY;
        bool facingRight = true;

        public GameObject ShurikenRight, ShurikenLeft;
        Vector2 ShurikenPos;
        public float throwRate = 0.5f;
        float nextThrow = 0f;

        Rigidbody2D rb;

        // Use this for initialization
        void Start() {
            rb = GetComponent<Rigidbody2D>();
            rb.freezeRotation = true;
        }

        // Update is called once per frame
        void Update() {
            if (Input.GetKey(KeyCode.RightArrow))
            {
                rb.AddForce(new Vector3(1, 0), ForceMode2D.Impulse);
            }
            else if (Input.GetKey(KeyCode.LeftArrow))
            {
                rb.AddForce(new Vector3(-1, 0), ForceMode2D.Impulse);
            }
            else if (Input.GetKey(KeyCode.UpArrow))
            {
                rb.AddForce(new Vector3(0, 2), ForceMode2D.Impulse);
                Debug.Log("Jump");
            }
            else if (Input.GetKey(KeyCode.DownArrow))
            {
                rb.AddForce(new Vector3(0, -3), ForceMode2D.Impulse);
                Debug.Log("Jump");
            }



            if (Input.GetButtonDown("Throw1") && Time.time > nextThrow)
            {
                nextThrow = Time.time + throwRate;
                Throw();
            }
        }

        void Throw()
        {
            ShurikenPos = transform.position;
            if (facingRight)
            {
                ShurikenPos += new Vector2(+2.2f, 3.355f);
                Instantiate(ShurikenRight, ShurikenPos, Quaternion.identity);
            }


            else
            {
                ShurikenPos += new Vector2(2.2f, 3.355f);
                Instantiate(ShurikenLeft, ShurikenPos, Quaternion.identity);
            }
        }
    }

Can anyone help me? Thank you in advance!

0 Answers
Related