Basically in the title. The other thing about it is that it only happens sometimes. If I am firing while moving backward, half the time the projectiles will go forward and the other half will go backward. I’m not exactly sure what could be causing it—hoping to get some help here.
This is the main code that is firing the projectiles
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
public class PlayerInteractions : MonoBehaviour
{
private PlayerStats playerStats;
[SerializeField] private Transform camera;
[SerializeField] private Transform attackPoint;
[SerializeField] private GameObject objectToThrow;
PhotonView PV;
private void Awake()
{
playerStats = GetComponent<PlayerStats>();
PV = GetComponent<PhotonView>();
}
// Update is called once per frame
void Update()
{
if(PV.IsMine)
{
Throw();
}
}
void Throw()
{
if(Input.GetButtonDown("Fire1") && playerStats.ThrowablesLeft > 0)
{
//Spawn projectile and grab Rigidbody
GameObject projectile = PhotonNetwork.Instantiate(objectToThrow.name, attackPoint.position, camera.rotation);
Rigidbody projectileRb = projectile.GetComponent<Rigidbody>();
//Calculate direction
Vector3 forceDirection = camera.transform.forward;
RaycastHit hit;
if(Physics.Raycast(camera.position, camera.forward, out hit, Mathf.Infinity))
{
forceDirection = (hit.point - attackPoint.position).normalized;
}
//Calculate force of throw and apply to projectile
Vector3 force = forceDirection * playerStats.ForwardThrowForce + transform.up * playerStats.UpwardThrowForce * Time.deltaTime;
projectileRb.AddForce(force, ForceMode.Impulse);
playerStats.ThrowablesLeft--;
}
}
}
This code is supplemental to the one above. I doubt its causing any issues but just to be safe
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
public class PlayerStats : MonoBehaviour
{
[SerializeField]
private int _health;
public int Health
{
get {
return _health;
}
set {
_health = value;
}
}
[SerializeField]
private float _speed;
public float Speed
{
get {
return _speed;
}
set {
_speed = value;
}
}
[SerializeField]
private float _jumpHeight;
public float JumpHeight
{
get {
return _jumpHeight;
}
}
[SerializeField]
private bool _hasDoubleJump;
public bool HasDoubleJump
{
get {
return _hasDoubleJump;
}
set {
_hasDoubleJump = value;
}
}
[SerializeField]
private bool _isPlayerGrounded;
public bool IsPlayerGrounded
{
get {
return _isPlayerGrounded;
}
set {
_isPlayerGrounded = value;
}
}
[SerializeField]
private float _forwardThrowForce;
public float ForwardThrowForce
{
get {
return _forwardThrowForce;
}
}
[SerializeField]
private float _upwardThrowForce;
public float UpwardThrowForce
{
get {
return _upwardThrowForce;
}
}
[SerializeField] private int _maxThrowables;
[SerializeField]
private int _throwablesLeft;
public int ThrowablesLeft
{
get {
return _throwablesLeft;
}
set {
_throwablesLeft = value;
if (_throwablesLeft > _maxThrowables)
{
_throwablesLeft = _maxThrowables;
}
}
}
[SerializeField]
private bool _hasSpeedBoost;
public bool HasSpeedBoost
{
get {
return _hasSpeedBoost;
}
set {
_hasSpeedBoost = value;
}
}
}
Any input would be appreciated!