Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Help with projectile

Discussion in 'Scripting' started by petizero, Jan 26, 2020.

  1. petizero

    petizero

    Joined:
    Jan 1, 2019
    Posts:
    47
    Hi
    So i got a particle that i would like to shoot out from a gun problem is it only moves on 1 axis no matter where i am facing. Any help?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ProjectileMove : MonoBehaviour
    6. {
    7.    
    8.     public float moveSpeed;
    9.     public float fireRate;
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.        
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         if(moveSpeed != 0)
    20.         {
    21.            transform.position += transform.forward * (moveSpeed * Time.deltaTime);
    22.         }
    23.     }
    24. }
    25.  
     
  2. petizero

    petizero

    Joined:
    Jan 1, 2019
    Posts:
    47
    Nvm i figured it out
     
  3. DeanTheodorakis

    DeanTheodorakis

    Joined:
    Apr 29, 2018
    Posts:
    53
    EDIT: glad you figured it out. sorry i hit post before refreshing.

    Is this in 2d or 3d? I can explain what I did in 2d if that will help to get the projectile to shoot elsewhere. but I'm not very familiar with 3d.

    either way, no matter which it is, what you will want is a Vector (either vector 2 or vector 3 depending on how many dimensions you are using) right now you have the transform.position which is a vector 3. added to that you have the transform.forward, which is also a vector 3. so that part looks fine (to me).

    However, you are also multiplying it by move speed, which is not a vector, but a float. I'm guessing that's where your error is.