- Home /
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);
}
}
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
bulletPoint has no value. Do your bullets happen to fly to the centre of the map?
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.
Are you instantiating your bullets inside another collider?
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.
Nope, this fix makes the bullets move towards the center, and even now, the fly away when I aim.
Hmm. you probably need to play around with which vectors to use. Definitely need to get away from using Ray for direction though.
Your answer
Follow this Question
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