- Home /
This question was
closed Mar 15, 2021 at 08:12 PM by
undevable for the following reason:
The question is answered, right answer was accepted
Question by
undevable · Mar 14, 2021 at 09:07 PM ·
c#scripting problemraycastshootingprojectile
My Projectile System has Another Bug
Hi everyone. I made a projectile system, however, there is 1 problem, each time I shoot, my bullet will fly out, however, if I look up, while the bullet is flying, the bullet will rise in height. What I want is that when I shoot my bullet and then look up in the sky, my bullet won't be visible (like every normal FPS Game). Can anyone please help me achieve this? My scripts are below:
Projectile Script:
using UnityEngine;
public class Projectile : MonoBehaviour
{
[Header("Customizable")]
public float projectileSpeed = 1f;
public float lifetime = 3f;
[Header("Setup")]
public Transform firePoint;
public Shooting shooting;
/// <summary>
/// The projectile force when shot in the air
/// </summary>
private float projectileForce = 500f;
private float _speed;
private Vector3 _target;
private void StartMovingTowards(Vector3 target, float speed)
{
_target = target;
_speed = speed;
}
public void Move(Projectile instance, Camera mainCam)
{
RaycastHit hit = shooting.hit;
if (hit.collider != null)
{
_target = hit.transform.position;
}
else
{
_target = mainCam.transform.forward * projectileForce;
}
instance.StartMovingTowards(_target, projectileSpeed);
}
private void FixedUpdate()
{
transform.position = Vector3.MoveTowards(transform.position, _target, _speed);
Destroy(gameObject, lifetime);
}
}
Shooting Script:
using UnityEngine;
public class Shooting : MonoBehaviour
{
[Header("Customizable")]
public float damage = 10f;
public float range = 100f;
public float fireRate = 15f;
public float impactForce = 30f;
[Header("Setup")]
public GameObject projectile;
public Camera fpsCam;
public Transform firePoint;
[HideInInspector]
public RaycastHit hit;
private float nextTimeToFire = 0f;
private void Update()
{
if (Input.GetButton("Fire1") && nextTimeToFire <= Time.time)
{
Shoot();
}
}
private void Raycast()
{
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
{
Debug.Log(hit.transform.name);
Target target = hit.transform.GetComponent<Target>();
if (target != null)
{
target.TakeDamage(damage);
}
if (hit.rigidbody != null)
{
hit.rigidbody.AddForce(-hit.normal * impactForce);
}
}
}
private void Projectile()
{
GameObject proj = Instantiate(projectile, firePoint);
Projectile script = proj.GetComponent<Projectile>();
if (script != null)
{
script.Move(script, fpsCam);
}
else
{
Debug.LogError("No Projectile.cs script attached to projectile Prefab!");
}
}
private void Shoot()
{
nextTimeToFire = Time.time + 1f / fireRate;
Raycast();
Projectile();
}
}
Thanks to anyone who answers. All help is greatly appreciated.
Comment
Best Answer
Answer by CoreDLL · Mar 14, 2021 at 11:14 PM
Row 50 use this instead: GameObject proj = Instantiate(projectile, firePoint.position, firepoint.rotation);