how to create adjustable speed for 2D shooting?
I've looked everywhere and I cant find a way to shoot a prefab bullet with adjustable speed. All the articles I found have the same solution in which the speed variable doesn't change anything. Every number I try causes the bullet to move at the same quick speed, even if it's zero or a negative. The only thing I've been able to do to change the speed is if I get rid of the speed variable completely. That causes the bullet to barely move. Does anyone know how to implement a working speed variable? Or is there an error with my code? This is my Shooting Script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Shooting : MonoBehaviour { public Transform firePoint; public GameObject bulletPrefab;
void Update()
{
if(Input.GetButtonDown("Fire1"))
{
Shoot();
}
}
void Shoot()
{
GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
}
}
and this is my bullet script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Bullet : MonoBehaviour {
public float speed = 0.1f;
public float damage = 5f;
public float Range = .1f;
public Rigidbody2D rb;
public Transform firePoint;
void Start()
{
rb.velocity = firePoint.up * speed;
Destroy(gameObject, 2f);
}
void OnCollisionEnter2D(Collision2D collision)
{
Destroy(gameObject);
}
}
Your answer
Follow this Question
Related Questions
Why won't my RigidBody 2D stop moving?, 0 Answers
Limiting the speed of my car 1 Answer
RigidBogy.AddForce does nothing. 0 Answers
Vuforia ARCamera doesn't rotate 1 Answer