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

shooting problem

Discussion in 'Scripting' started by chmm, May 24, 2020.

  1. chmm

    chmm

    Joined:
    Apr 29, 2020
    Posts:
    2
    hi all,

    i'm starting programing, sorry if its an easy issue.

    i have a problem whit my script.

    If i press right arrow i can shoot.
    If i press left arrow i can shoot.
    if i press up and right arrow i can shoot..
    but i i press up and left arrow, down and left, down and right i can't shoot.

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

    public class Player : MonoBehaviour
    {
    [SerializeField] private float _impulse= 5.0f;
    [SerializeField] private float _rotspeed = 2.0f;
    [SerializeField] private float _maxSpeed = 8.0f;
    public GameObject laserPrefab;
    private Rigidbody playerRb;


    // Start is called before the first frame update
    void Start()
    {
    playerRb = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {

    Shooting();
    Movement();
    }

    void Movement()
    {
    if (Input.GetKey(KeyCode.UpArrow))
    {
    playerRb.AddRelativeForce(Vector3.forward * _impulse * Time.deltaTime, ForceMode.Impulse);
    }

    if(Input.GetKey(KeyCode.DownArrow))
    {
    playerRb.AddRelativeForce(Vector3.back * _impulse * Time.deltaTime, ForceMode.Impulse);
    }


    transform.Rotate(0.0f, Input.GetAxis("Horizontal") * _rotspeed, 0.0f);


    playerRb.velocity = Vector3.ClampMagnitude(playerRb.velocity, _maxSpeed);

    }

    void Shooting()
    {

    if (Input.GetKeyDown(KeyCode.Space))
    {
    Instantiate(laserPrefab, gameObject.transform.localPosition, gameObject.transform.rotation);
    }
    }




    }
     
  2. chmm

    chmm

    Joined:
    Apr 29, 2020
    Posts:
    2
    Solved, changing fire button works....don't understand but its ok!