- Home /
 
Getting the Bullet to add the Shooters speed to it's own
Hi, I'm working on a bullet system were the bullets, for only when they spawn, obtain the shooters initial 'velocity' and add it to it's own 'bullet speed' that way the shooter can never catch up to the bullet,(assuming the shooter doesn't find a way to move faster after it fires). The problem I'm having is getting the bullet to add the shooters velocity to it's own speed. The bullet gets instantiated like I want but I don't know how to get it to find the shooter once it's created. I tried using get and set's but I'm unfamiliar with them. I managed to get the bullets to gets the players direction so it knows were to fire but not it's velocity.
If someone could point me in the right direction with how I should code or help me with my code I would be really grateful.
 using UnityEngine;
 using System.Collections;
 
 public class PlayerMove : MonoBehaviour {
     
     public int moveDirection     = 1; //direction the player is facing
     public Vector3 velocity = Vector3.zero; //speed of the player and direction
     
     void Update ()
     {    
         rigidbody.velocity = velocity;
     }
 }
 
 using UnityEngine;
 using System.Collections;
 
 public enum ShooterType{
     Player,
     Enemy
 }
 public class Bullet : MonoBehaviour {
 
     public  float     _mySpeed;
     private float     _myDirection = 1f;
     private  float     _shooterSpeed;
     
     //Stuff you change per bullet
     public float     _projectileSpeed= 18f;
     
     PlayerMove _PM;
     EnemyStats _ES;
     public ShooterType _shooterType;
 
     void Start () {
         BulletDirection();
     }
 
     void Update () {
         BulletSpeed();
     }
     
     void BulletSpeed() {
         SwitchClass(_shooterSpeed);
         if (_shooterSpeed < 0) {
             _shooterSpeed *= -1;
         }
         _mySpeed =  (_shooterSpeed + _projectileSpeed) * _myDirection;
     }
     void BulletDirection(){
         if(_shooterType == ShooterType.Player){
             if (_PM.moveDirection == 0){
                 _myDirection = -1;
             }
         }else if (_shooterType == ShooterType.Enemy){
             if (_ES._front == false){
                 _myDirection = -1;
             }
         }
     }
 
     void SwitchClass(float _type){
         switch (_shooterType){
         case ShooterType.Player :
             _type = _PM.velocity.x;
             break;
         case ShooterType.Enemy :
             _type = _ES._velocity.x;
             break;
         }
     }
     
     public PlayerMove PlayerObj{
         set {_PM = value;}
         get {return _PM;}
     }/*
     public EnemyStats EnemyObj{
         set {_ES = value;}
         get {return _ES;}
     }*/
 }
 
              Your answer