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);
}
}