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 can I control the bullet movement speed but keep the distance using physics ?

Discussion in 'Scripting' started by DubiDuboni, Jun 8, 2020.

  1. DubiDuboni

    DubiDuboni

    Joined:
    Feb 5, 2019
    Posts:
    131
    this shoot bullets to specific distance for example 100.
    I want to be able to control also the speed so it will keep moving to distance 100 or 50 or 200 but on specific speed for example speed 1 or speed 10 or speed 100.

    now it's moving shooting the bullets very fast and I can control only the distance but not the speed.

    another problem the bullets have a Rigidbody attached so maybe the function Fire should be called from FixedUpdate ?

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Linq;
    5. using System.Threading;
    6. using UnityEditor;
    7. using UnityEngine;
    8.  
    9. public class ShootBullets : MonoBehaviour
    10. {
    11.     public float bulletDistance;
    12.     public bool automaticFire = false;
    13.     public float fireRate;
    14.     public Rigidbody bullet;
    15.  
    16.     private float gunheat;
    17.     private bool shoot = false;
    18.     private GameObject bulletsParent;
    19.     private GameObject[] startpos;
    20.  
    21.     // Start is called before the first frame update
    22.     void Start()
    23.     {
    24.         bulletsParent = GameObject.Find("Bullets");
    25.         startpos = GameObject.FindGameObjectsWithTag("Pod_Weapon");
    26.     }
    27.  
    28.     void Fire()
    29.     {
    30.         for (int i = 0; i < startpos.Length; i++)
    31.         {
    32.             Rigidbody bulletClone = (Rigidbody)Instantiate(bullet, startpos[i].transform.position, startpos[i].transform.rotation, bulletsParent.transform);
    33.  
    34.             bulletClone.velocity = transform.forward * bulletDistance;
    35.             // The bullet or any ammo/weapon should be on Y rotation = 0
    36.             Destroy(bulletClone.gameObject, 0.5f);
    37.         }
    38.     }
    39.  
    40.     // Update is called once per frame
    41.     void Update()
    42.     {
    43.         if (automaticFire == false)
    44.         {
    45.             if (Input.GetMouseButtonDown(0))
    46.             {
    47.                 Fire();
    48.             }
    49.         }
    50.         else
    51.         {
    52.             if (shoot == true)
    53.             {
    54.                     Fire();
    55.  
    56.                     shoot = false;
    57.             }
    58.         }
    59.  
    60.         if (gunheat > 0) gunheat -= Time.deltaTime;
    61.  
    62.         if (gunheat <= 0)
    63.         {
    64.             shoot = true;
    65.             gunheat = fireRate;
    66.         }
    67.     }
    68. }
    69.  
     
  2. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    352
    Simple... Get the point you want it to move to. Then use the MoveTowards function. So something like this.

    Code (CSharp):
    1. vector3 targetPos = player.Transform.forward * distance;
    2. bullet.transform.position = vector3.MoveTowards(bullet.transform.position, targetPos, speed * time.DeltaTime);
    3. if (bullet.transform.position == targetPos){
    4.     // Bullet reached distination
    5. }
    Rigidbody is fine, I'm assuming you have it as kinematic??
    Because you're not using the physics system to move the bullet, you don't need it in update.

    However, keep in mind. With things like live bullets where they move in the world, and your detecting real collision. It can get a bit hairy because the bullets move so fast. FixedUpdate wont always fire in time. So something like on OnCollisionEnter/OnTrigger enter may never fire.

    Most games like COD etc, dont actually have moving bullets. They just have affects. And its a raycast that detects if the bullet hits something.
     
  3. armanaramesh

    armanaramesh

    Joined:
    Mar 12, 2021
    Posts:
    1
    this is my code for bullets that move down , (2d)
    this script specially is for bullet Prefab, you can use it in your enemy player object and instansiate it in random time,
    or you can use it in your player and when click on fire button , instansiate it in player gun position.
    enjoy it ;)

    Code (CSharp):
    1.  
    2.     public float speed;
    3.     public GameObject Explosion;
    4.     public int power;
    5.     private PlayerController player;
    6.  
    7.     private void Awake()
    8.     {
    9.         player = FindObjectOfType<PlayerController>();  // the Player
    10.     }
    11.  
    12.     private void Update()
    13.     {
    14.         TrackingPlayer();
    15.     }
    16.  
    17.     private void TrackingPlayer()
    18.     {
    19.         Vector3 targetPos = player.gameObject.GetComponent<Transform>().position;  // player position
    20.         transform.position = Vector3.MoveTowards(transform.position, targetPos, Time.deltaTime * speed);  //tracking Player
    21.         Vector3 dir = targetPos - transform.position;    // direction between bullet And player
    22.         float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;  // the angle between player & bullet(degree) relate to +x axis.
    23.         Quaternion q = Quaternion.Euler(0f, 0f, angle + 90); // bullet move down ; if bullet move up : 90 - angle
    24.         transform.rotation = q;
    25.     }
    26.     private void OnCollisionEnter2D(Collision2D col)
    27.     {
    28.         if (col.gameObject.tag == "Player")
    29.         {
    30.             Instantiate(Explosion, col.GetContact(0).point, Quaternion.identity);
    31.             Destroy(gameObject);
    32.         }
    33.     }