Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Noxide7 · Jun 22, 2014 at 11:15 PM · shootingbullet

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;}
     }*/
 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

bullets are spawning under me and they don't move 1 Answer

Random Raycast inside crosshairs 1 Answer

How do I make the gun stop shooting when I am out of ammo? 1 Answer

How to kill an object when I shoot bullets 1 Answer

Best way to shoot physical bullets? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges