Raycast shooting impact effect position problem
I am working on a Multiplayer FPS, I followed Brackey's multiplayer tutorial (parts of it) and got stuck on the impact effect.
For some reason, the impact particle prefab is only instantiating at the center, not the impact point.
Here's my code:
(Attached to Player Prefab)
PlayerShooting.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class PlayerShooting : NetworkBehaviour
{
public PlayerWeapons weapon; //Stores values of the weapon
public PlayerAnimationNetwork playerAnimationNetwork; //Animation Network, MuzzleFlash effect
public WeaponGFX wpgfx; //Another Script
[SerializeField]
private LayerMask mask;
[SerializeField]
private Camera cam; //player's camera
private bool Delay; //shooting delay
public int animShoot; //used to control PlayerAnimationNetwork
void Start()
{
if (cam == null)
{
Debug.LogError("No Camera Reference");
this.enabled = false;
}
}
void Update()
{
if (Input.GetButtonDown("Fire1") && Delay == false)
{
Shoot();
}
}
//Console command stuff, executes the particles and impacts on other clients
[Command]
void CmdOnShoot()
{
RpcDoMuzzleFlash();
}
[Command]
void CmdOnHit(Vector3 _pos)
{
RpcDoImpact(_pos);
}
//Console executes these commands to tell the clients what to play and what to instantiate
[ClientRpc]
void RpcDoMuzzleFlash()
{
playerAnimationNetwork.ps.Play();
playerAnimationNetwork.sound.Play();
}
[ClientRpc]
void RpcDoImpact(Vector3 _pos)
{
GameObject _hitEffect = Instantiate(wpgfx.hitEffectPrefab, _pos, Quaternion.identity);
Destroy(_hitEffect, 0.5f);
}
//LocalPlayer Stuff
[Client]
void Shoot()
{
if (!isLocalPlayer)
{
return;
}
else
{
RaycastHit _hit;
if (Physics.Raycast(cam.transform.position, cam.transform.forward, out _hit, weapon.range, mask))
{
// We hit sth
}
CmdOnShoot();
CmdOnHit(_hit.point);
Delay = true;
animShoot = 1;
Invoke("falseDelay", 0.12f);
}
}
void falseDelay()
{
Delay = false;
animShoot = 0;
}
}
Answer by streeetwalker · Sep 24, 2020 at 10:30 AM
HI SwiftKraft, You seem to have a problem with this bit of code:
RaycastHit _hit;
if (Physics.Raycast(cam.transform.position, cam.transform.forward, out _hit, weapon.range, mask))
{
// We hit sth
}
CmdOnShoot();
CmdOnHit(_hit.point)
Your Raycast result has no bearing on your call to the function that will ultimately instantiate your impact effect. Chances are you are not getting any hits, and if you don't you are passing (0,0,0) as the hit point - hence the position of the impact effect is at the center of the world.
Perhaps you made a mistake following the tutorial. I haven't looked at the tutorial, but my guess is those last two lines of code need to be in the body of the if physics.Raycast block, not after it.
I tried it but it didn't instantiate anything. even if I am literally shooting the ground
Yes, that means your raycast is not working the way you think it should. There could be a number of reasons why, but it is impossible to tell without seeing the complete set up of the objects you are supposed to be hitting.
So now that the if statement is set up properly, you have further problems.
One thing I just spotted that looks suspicious is the mask variable - have you given your objects a layer, and is the mask set up to catch that? I don't see in your code where you are assigning a value to the mask. I would set it up as a public variable and chose the layers in the inspectors popup field.
Once you have that problem resolved, double check weapon.range.
Thanks a lot! I assigned the mask variable on the inspector, the hit effects started showing on the correct places!
Your answer
Follow this Question
Related Questions
Raycast not drawing where i tell it 1 Answer
Instantiating object in the wrong place,Instantiate object at mouse point getting wrong coordinates 1 Answer
Issue with Raycast hit.distance Variables Reseting Itself 0 Answers
Bullets with raycast and physics 2 Answers
NetworkServer.Spawn spawns objects differently between clients 0 Answers