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 RiddarN · Feb 19, 2015 at 03:17 PM · c#rotationvector3bulletweapon

Bullet flies away for no apparent reason.

Hello!

EDIT: I've noticed that some bullets actually goes where they should, it is only the majority of bullets that strafe off

I am creating an FPS shooter where I spawn the bullet at a bulletpoint and make it fly towards the middle of the screen. All the code for this is done and working, but when I aim, I get some weird bullet logic happenings.
Here is a video of what I mean

And here is my code.

CREATEBULLET METHOD

     public void CreateBullet()
     {
         // This will store the raycast variables that is 
         // needed to correct the rotation of the bullet
         // aswell as the bulets final position
         Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2,Screen.height / 2,0));
         // Test this later :..: Ray ray = new Ray(Camera.main.transform.position, Vector3.forward);
         RaycastHit hit;
 
 
         // This will instantiate (create) the bullet
         // It is created at the position of the bulletpoint
         // It is created with the rotation of the camera
         GameObject newBullet = (GameObject)Instantiate(bulletType, bulletPoint.transform.position, transform.rotation);
 
         // This will cast a ray from the camera
         // The ray will hit something and save the hitpoint
         // The bullet will be rotated to that point and fly towards it
         // This will make the bullet always fly towards the middle of the screen
         // The raycast is measured in "meters"
 
         if (Physics.Raycast(ray,out hit,500000))
         {
             Debug.Log(hit.point);
             newBullet.transform.LookAt(hit.point);
         } 
 
         // Sets the aftermath of the bullet
         // Such as bulletholes
         newBullet.GetComponent<Bullet>().bulletHoleLifespawn = bulletHoleLifespawn;
         newBullet.GetComponent<Bullet>().bulletHole = bulletHole;
 
         // Sets the bullet lifespawn (lifetime)
         // Lifespawn is counted in seconds
         newBullet.GetComponent<Bullet>().lifespawn = bulletLifespawn;
 
         // This will set the damage o the bullet
         newBullet.GetComponent<Bullet>().bulletDamage = bulletDamage;
         
         // Sets the bulletspeed
         newBullet.GetComponent<Bullet>().bulletSpeed = bulletSpeed;
 
         // This will set the bullets force on physic objects
         // E.G the force a weapon is pushed by when shot
         newBullet.GetComponent<Bullet>().effectOnPhysics = effectOnPhysics;
 
         // This lets the bullet know what gun fired it
         newBullet.GetComponent<Bullet>().weapon = gameObject;
 
         // This will remove one bullet from the weapon
         // This should not be anywhere else in this script
         bullets--;
     }
 }


AIM METHOD

     public void AimLogic()
     {
         if(canAim)
         {
             // Checks if the player wants to aim
             // First is for aimtoggle
             // Second is for the "isAiming" variable
             if (aimToggle && inputAim)
             {
                 wantAim = !wantAim;
             }
             
             if (wantAim || aimInput == 1)
             {
                 // Lets all classes know that
                 // This gun is aiming
                 // Also makes the gun actually aim
                 isAiming = true;
             }
             else if (!wantAim || aimInput == 0)
             {
                 // Makes the gun know when to stop aiming
                 // Disables the aim
                 isAiming = false;
             }
 
             if (isAiming)
             {
                 // Setups all the diffrent aimsettings
                 if (scopedAim)
                 {
                     chestRig.GetComponent<ChestrigScript>().wantScopeAim = true;
                 }
                 if (noGunAim)
                 {
                     hideGun = true;
                 }
                 if (noCrosshairAim)
                 {
                     chestRig.GetComponent<ChestrigScript>().hideCrosshair = true;
                 }
                 // moves the gun to the aim offset with the aim rotation
                 transform.localPosition = aimOffset;
                 transform.localRotation = Quaternion.Euler(aimRotation);
 
                 // Sets the camera FOV's
                 Camera.main.fieldOfView = aimFOV;
             }
             else if (!isAiming)
             {
                 if(noGunAim)
                     hideGun = false;
                 chestRig.GetComponent<ChestrigScript>().wantScopeAim = false;
                 chestRig.GetComponent<ChestrigScript>().hideCrosshair = false;
                 Camera.main.fieldOfView = standardFov;
             }
         }
     }

BULLETSCRIPT

 using UnityEngine;
 using System.Collections;
 
 public class Bullet : MonoBehaviour {
 
     // These can be modified
     public GameObject bulletHole;
 
     // This is automatically modified by the weaponscript
     public GameObject weapon;
     public float bulletSpeed;
     
     public float bulletDamage = 50.0f;
     public float effectOnPhysics = 200.0f;
     public float bulletHoleLifespawn = 6.0f;
     public float lifespawn = 10.0f;
 
     // Use this for initialization
     void Start () {
         transform.position = weapon.GetComponent<WeaponScript>().bulletPoint.gameObject.transform.position;
     }
     
     // Update is called once per frame
     void Update () {
         RaycastHit rayHit; 
 
         if (Physics.Raycast(transform.position, transform.forward, out rayHit, bulletSpeed * Time.deltaTime))
         {
             EnemyHit(rayHit);
             WeaponHit(rayHit);
 
             // Here is generall hit code
         }
 
         // Here is the update
         Debug.Log (transform.rotation);
 
         // This is the script that will make the bullet move forward
         transform.Translate(Vector3.forward * bulletSpeed * Time.deltaTime);
 
         // This will take care of the bullet lifespawn
         // It will take off some of the lifespawn flaot until
         // it reaches 0
         // Then it will destroy (remove) this object entirely
         lifespawn -= Time.deltaTime;
         if (lifespawn <= 0)
             GameObject.Destroy(gameObject);
 
     }
     // // // // //
     //  Methods //
     // // // // //
 
     public void EnemyHit(RaycastHit rayHit)
     {
         // In here is all the collisions that may happen with the bullet
         if (rayHit.collider.tag == "Enemy")
         {
             rayHit.collider.gameObject.GetComponent<Health>().entityHealth -= bulletDamage;
             GameObject.Destroy(gameObject);
         }
     }
 
     public void WeaponHit(RaycastHit rayHit)
     {
         if (rayHit.collider.tag != "Weapon" && rayHit.collider.tag != "Player")
         {
             // Creates a new bullet hole
             GameObject newBulletHole = (GameObject)Instantiate(bulletHole, rayHit.point, Quaternion.FromToRotation(Vector3.up, rayHit.normal));
             // Sets the parent of the newest bullethole
             newBulletHole.transform.parent = rayHit.transform;
             // Removes the bullethole after some time
             GameObject.Destroy(newBulletHole, bulletHoleLifespawn);
             // Removes the bullet
             GameObject.Destroy(gameObject);
         }
         
         if (rayHit.collider.tag == "Weapon")
             rayHit.rigidbody.AddForceAtPosition(transform.forward * effectOnPhysics, rayHit.point);
     }
 }
 










Comment
Add comment · Show 5
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
avatar image melkorinos · Feb 19, 2015 at 03:33 PM 0
Share

You should post only the part of the code that is moving the bullet, or is affecting it, this is 500 lines of code. Just friendly advice, no hate :D

avatar image meat5000 ♦ · Feb 19, 2015 at 03:42 PM 0
Share

bulletPoint has no value. Do your bullets happen to fly to the centre of the map?

avatar image RiddarN · Feb 19, 2015 at 08:58 PM 0
Share

I'll keep in $$anonymous$$d only to post relative code in the future. $$anonymous$$ight even update this post,but I'm not sure what is relative right now as he bullet should be working.

As for the bulletpoint, it does have a value. It's hatched and fully working.

avatar image AlucardJay · Feb 20, 2015 at 12:47 PM 0
Share

Are you instantiating your bullets inside another collider?

avatar image RiddarN · Feb 22, 2015 at 10:35 AM 0
Share

I am not.

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by rageingnonsense · Feb 19, 2015 at 09:00 PM

What happens if you do not hit anything? In that instance, you'll never have a hit point, and your bullets are probably going to look at 0,0,0. what you are seeing is your bullets moving towards the scene origin.

Instead of using a ray, how about:

 newBullet.transform.forward = camera.main.transform.forward

or

 newBullet.Lookat(camera.main.transform.forward * 2);

In the second example, we multiple forward by 2 to get a point away from the camera, but in that direction.

You can use a ray to determine if it would hit anything, but you do not need it to set the projectile in motion in a particular direction.

TL;DR: Using a ray for this is inappropriate.

Comment
Add comment · Show 6 · Share
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
avatar image RiddarN · Feb 19, 2015 at 09:03 PM 0
Share

I will try this fix! Seems like a reasonable argument.

avatar image RiddarN · Feb 19, 2015 at 09:11 PM 0
Share

Nope, this fix makes the bullets move towards the center, and even now, the fly away when I aim.

avatar image rageingnonsense · Feb 19, 2015 at 09:23 PM 0
Share

Hmm. you probably need to play around with which vectors to use. Definitely need to get away from using Ray for direction though.

avatar image rageingnonsense · Feb 19, 2015 at 09:23 PM 0
Share

Also, you should post your Bullet script.

avatar image RiddarN · Feb 20, 2015 at 08:36 AM 0
Share

$$anonymous$$y bulletscript is already posted :)

Show more comments

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Flip over an object (smooth transition) 3 Answers

Spacesim AI pilot, vector3.cross issues 1 Answer

Distribute terrain in zones 3 Answers

Making my GambeObject rotate to face the direction of movement, smoothly. 0 Answers

2d game facing bullets to mouse 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