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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Controlling particles hit point with code?

Discussion in 'Scripting' started by Kovenant, Sep 24, 2017.

  1. Kovenant

    Kovenant

    Joined:
    Sep 18, 2013
    Posts:
    254
    When I fire a weapon in my game there's bullet spread. I shoot "bullets" wity Raycast.. When I shoot I activate a muzzle flash, and on this muzzle flash PS I've added one particle that flies away with high speed leaving a trail behind (to simulate a bullet flying through the air). Now, this bullet trail goes straight forward each time; not ending up where my raycast bullets end up - making it look a bit weird...

    Can I control that particular particle to end up where the raycast bullets end up (hit.point)? If so, how?
    Darn, I hope you guys understand what I mean:D

    Code (CSharp):
    1. private void Update()
    2.     {
    3.         if (!canShoot)
    4.             return;
    5.  
    6.         ellapsedTime += Time.deltaTime;
    7.  
    8.         if(controller.m_Input.x != 0 || controller.m_Input.y != 0)
    9.         {
    10.             bulletSpread = (normalSpread * spreadMultiplierWhenMoving);
    11.         } else
    12.         {
    13.             bulletSpread = normalSpread;
    14.         }
    15.  
    16.         if (Input.GetButton("Fire1") && ellapsedTime > shotCooldown)
    17.         {
    18.             Vector3 spreadDirection = firePosition.forward;
    19.  
    20.             ellapsedTime = 0f;
    21.             CmdFireShot(firePosition.position, spreadDirection + Random.insideUnitSphere * bulletSpread);
    22.         }
    23.     }
    24.  
    25.     [Command]
    26.     void CmdFireShot(Vector3 origin, Vector3 direction)
    27.     {
    28.         Ray ray = new Ray(origin, direction);
    29.         Debug.DrawRay(ray.origin, ray.direction * 3f, Color.red);
    30.  
    31.         bool result = Physics.Raycast(ray, out hit, 150f);
    32.         bool hitEnemy = false;
    33.  
    34.         if(result)
    35.         {
    36.             PlayerHealth enemy = hit.transform.GetComponent<PlayerHealth>();
    37.  
    38.             if(enemy != null)
    39.             {
    40.                 hitEnemy = true;
    41.                 bool wasKillShot = enemy.TakeDamage(transform.position, Random.Range(bulletDamage.x, bulletDamage.y));
    42.                 if (wasKillShot)
    43.                     score++;
    44.             } else
    45.             {
    46.                 hitEnemy = false;
    47.             }
    48.         }
    I ofc understand that I need to referense that Particle Effect, but.. then what? :)
     
    Last edited: Sep 24, 2017
  2. Kovenant

    Kovenant

    Joined:
    Sep 18, 2013
    Posts:
    254
    Oh yeah; forgot to mention.. This needs to be done over network. I have tried creating a empty GameObject with a Trail Render component as a prefab = it needs to be instantiated with NetworkServer.Spawn() and the position of the prefab needs to be synced over the network which is impossible since it would be moving so fast; so the best solution would be to make this a part of the particle system of the muzzle flash and control where that BulletTrail particle ends up...

    If it was a Single player offline game, the prefab would work perfectly thou...