#Ranged weapon

1 messages Β· Page 1 of 1 (latest)

serene apex
#

Thread.

undone sentinel
#

ahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh

#

πŸ˜‚ πŸ˜‚ πŸ˜‚ πŸ˜‚ πŸ˜‚ πŸ˜‚ πŸ˜‚ πŸ˜‚ πŸ˜‚ πŸ˜‚ πŸ˜‚ πŸ˜‚ πŸ˜‚ πŸ˜‚

sonic linden
#

Ok, and what triggers the Shoot action

undone sentinel
sonic linden
#

What has to happen to trigger the Shoot action and make the gun or whatever it is shoot bullets

#

There has to be some kind of trigger, it won't just shoot by itself.

undone sentinel
ember matrix
#

Where is the code for such actions

sonic linden
#

Show it

undone sentinel
#
using System.Collections;
using UnityEngine;
using StarterAssets;
public class Gun : MonoBehaviour
{
    private StarterAssetsInputs _input;
    [SerializeField] private GameObject bulletPrefab;
    [SerializeField] private GameObject bulletPoint;
    [SerializeField] private float bulletSpeed = 600;

    void Start()
    {
        _input = transform.root.GetComponent<StarterAssetsInputs>();
    }

    // Update is called once per frame
    void Update()
    {
        if (_input.shoot)
        {
            Shoot();
            _input.shoot = false;
        }
    }

    void Shoot()
    {
        Debug.Log("shoot!");
        GameObject bullet = Instantiate(bulletPrefab, bulletPoint.transform.position, transform.rotation);
        bullet.GetComponent<Rigidbody>().AddForce(transform.forward * bulletSpeed);
        Destroy(bullet, 1);
    }
}```
#

That's all the code

sonic linden
#

Ok, can't really help since it's the new Input System, haven't been using it at all UnityChanClever

ember matrix
#

Old input system best

sonic linden
#

+1

#

The code with the old input system would look something like this

if (Input.GetKey(KeyCode.P))
{ 
  Shoot();
}
#
  • maybe some cooldown