Question by
unity_eBwWrmIw8wobPA · Oct 19, 2020 at 09:34 AM ·
unity 5raycastshootingraycastingraycasthit
Raycast bug is making me go nnuts,Raycast bug is making me go nuts
The problem basically is that this code is supposed to make the raycast go forward with respect to the camera, but for some reason the freaking raycast even though I point upwards or down, it ONLY goes straight. Please help me out cause I am soo close to giving up on my project
using Photon.Pun; using System.Collections; using System.Collections.Generic; using UnityEngine; using Photon.Realtime; using TMPro; using EZCameraShake; using JetBrains.Annotations; using UnityEngine.UI;
public class PistolShoot : MonoBehaviourPunCallbacks {
//This is a pistol
private int damage = 5;
public float range = 100f;
public ParticleSystem muzzleFlash;
public Transform startPos;
public Camera fpsCam;
public Recoil recoil;
public float amount;
public float maxAmount;
public float smoothAmount;
public float rotationAmount = 4f;
public float smoothRotation = 12f;
public float maxrotationAmount = 12f;
public int opponentHP;
public int Kills;
public GameObject manager;
public float NextFire;
[SerializeField] private GameObject trail;
[SerializeField] Transform TrailPos;
private Vector3 intialPosition;
public Quaternion initalRotation;
[SerializeField] private TextMeshProUGUI tmp;
private float movementX;
private float movementY;
public GameObject lastShot;
[Space]
public bool rotationX = true;
public bool rotationY = true;
public bool rotationZ = true;
public bool shooting = false;
public GameObject playerShot;
public float maxDeviation = 2;
public float Firerate = 0.5f;
public GameObject player;
[SerializeField] bool opponentkilled;
[SerializeField] ParticleSystem spawnEffect;
public Health hp;
[SerializeField]private bool isAuto;
[SerializeField] private Text message;
public int Pistolammo = 50;
public int Rifleammo = 100;
private bool pistolActive;
private bool rifleActive;
[SerializeField] private GameObject hitEffect;
[SerializeField] private Transform startPoint;
[SerializeField] Transform aimPosition;
public void Start()
{
manager = GameObject.Find("ScoreManager");
intialPosition = transform.localPosition;
initalRotation = transform.localRotation;
if (transform.root.GetComponent<PhotonView>().IsMine)
{
hp = transform.root.GetComponent<Health>();
player = transform.root.gameObject;
}
/* if (transform.gameObject.GetComponent<PhotonView>().IsMine)
{
tmp = GameObject.Find("HUD/Health/Kills").GetComponent<TextMeshProUGUI>();
tmp.text = "Kills " + Kills;
}*/
if (photonView.IsMine)
{
}
}
void Update()
{
/*if (transform.gameObject.GetComponent<PhotonView>().IsMine)
{
tmp.text = "Kills " + Kills;
}*/
Sway();
if (Pause.paused && photonView.IsMine) return;
if (isAuto)
{
if (Input.GetButton("Fire1") && Time.time >= NextFire)
{
if (photonView.IsMine)
{
pistolActive = false;
rifleActive = true;
if (Rifleammo > 0)
{
NextFire = Time.time + 1 / Firerate;
photonView.RPC("Shooting", RpcTarget.AllBuffered);
Rifleammo--;
}
else if(Rifleammo <= 0)
{
return;
}
}
}
}
if (Input.GetKey(KeyCode.Mouse1))
{
if (photonView.IsMine)
{
gameObject.transform.position = Vector3.Lerp(gameObject.transform.position, aimPosition.position, Time.deltaTime * 2f);
}
}
else
{
if (Input.GetButtonDown("Fire1"))
{
if (photonView.IsMine)
{
pistolActive = true;
rifleActive = false;
if (Pistolammo > 0)
{
NextFire = Time.time + 1 / Firerate;
photonView.RPC("Shooting", RpcTarget.AllBuffered);
Pistolammo--;
}
else if (Pistolammo <= 0)
{
return;
}
}
}
}
}
public void Sway()
{
float movementX = Input.GetAxis("Mouse X") * amount;
float movementY = Input.GetAxis("Mouse Y") * amount;
movementX = Mathf.Clamp(movementX, -maxAmount, maxAmount);
movementY = Mathf.Clamp(movementY, -maxAmount, maxAmount);
Vector3 finalPosition = new Vector3(movementX, movementY, 0);
transform.localPosition = Vector3.Lerp(transform.localPosition, finalPosition + intialPosition, Time.deltaTime * smoothAmount);
}
[PunRPC]
public void Shooting()
{
/*Vector3 forwardVector = Vector3.forward;
float deviation = Random.Range(0f, maxDeviation);
float angle = Random.Range(0f, 360f);
forwardVector = Quaternion.AngleAxis(deviation, Vector3.up) * forwardVector;
forwardVector = Quaternion.AngleAxis(angle, Vector3.forward) * forwardVector;
forwardVector = fpsCam.transform.rotation * forwardVector;*/
recoil.Fire();
muzzleFlash.Play();
RaycastHit hit;
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
{
if (hit.collider.gameObject != null)
{
Debug.Log(hit.collider.gameObject.name);
}
/*transform.LookAt(fpsCam.transform.forward);*/
GameObject g = Instantiate(trail.gameObject, TrailPos.position, Quaternion.identity);
g.GetComponent<TrailRenderer>().AddPosition(hit.point);
if (hit.collider.gameObject.layer == 17 || hit.collider.gameObject.layer == 18)
{
Kills = Kills + 20;
if (hit.collider.gameObject.GetComponent<Health>() != null)
{
hit.collider.gameObject.GetComponent<Health>().TakeDamage(20);
}
Debug.Log("Hit player lol");
shooting = true;
lastShot = hit.collider.gameObject;
Debug.Log(lastShot.name);
}
GameObject impactEffect = Instantiate(hitEffect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(impactEffect, 2f);
}
}
private void TiltSway()
{
float tiltX = Mathf.Clamp(movementX * rotationAmount, -maxrotationAmount, maxrotationAmount);
float tiltY = Mathf.Clamp(movementY * rotationAmount, -maxrotationAmount, maxrotationAmount);
Quaternion finalRotation = Quaternion.Euler(new Vector3(
rotationX ? tiltX : 0f, rotationY ? tiltY : 0f, rotationZ ? tiltY : 0f));
transform.localRotation = Quaternion.Slerp(transform.localRotation, finalRotation * initalRotation, Time.deltaTime * smoothAmount);
}
void OnGUI()
{
if (photonView.IsMine)
{
GUI.Box(new Rect(10, 40, 100, 30), "XP | " + Kills);
if (rifleActive)
{
GUI.Box(new Rect(10, 70, 100, 30), "Rifle | " + Rifleammo);
}
else if (pistolActive)
{
GUI.Box(new Rect(10, 100, 100, 30), "Pistol | " + Pistolammo);
}
}
}
},
Comment