- Home /
[FPSGame] gun's muzzlePoint rotation Update Problem
my biggest problem is :
I am making a commercial FPS Game and i am stuck on MuzzlePoint's Rotation. i want my Bullet Tracer go from the muzzlePoint's current location to center of the screen (crosshair's center).
my Incomplete Script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class AssaultRifle : MonoBehaviour {
public float currentBullets;
public float bulletsPerMag;
public float fireRate;
public GameObject muzzlePoint;
public Camera FPSCamera;
float firetimer;
bool isReloading;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKey (KeyCode.Mouse0) && currentBullets < 0) {
Fire ();
}
}
void Fire()
{
if (isReloading)
return;
RaycastHit hit;
if (Physics.Raycast (FPSCamera.transform.position, FPSCamera.transform.forward)) {
// What to do here ????
// muzzlePoint.transform.localRotation = hit.point; Failed!!! :(
}
if (firetimer < fireRate)
firetimer = Time.deltaTime;
}
}
all i want to do is keep the rotation of MuzzlePoint to the raycasthit hit's hit Location..
plzz helpp !!! :(
Answer by SonicScrew12 · Nov 18, 2017 at 09:23 PM
Firstly, hit.point is of type Vector3, meaning it returns a single point in 3D space. You're trying to apply a rotation (which is of type Quaternion) from a Vector3 so you're getting a type mismatch. You can't directly cast a quaternion to a Vector3, you need to multiply it by a vector.
Now, if all you want to do is make the bullet tracer go from the muzzle point to the point where the raycast hit, you could just do this (assuming you're rotating the camera and the player independently):
GameObject bulletTracer = Instantiate(bulletTracerPrefab, muzzlePoint.transform.position, Quaternion.Euler(FPSCamera.transform.rotation.eulerAngles.x, player.transform.rotation.eulerAngles.y, 0f) );
Also assuming you have a GameObject called 'player' so it can get the euler angle from that object. I've just tried this and it works fine.
I hope I was able to solve your issue. Good luck, and happy coding.
Answer by DevmobiStudios · Nov 19, 2017 at 07:28 PM
@SonicScrew12 I Didn't mean that !!
I Mean i want to instantiate the tracer at the gun's end and move it to the center of the screen and at the same time, making it charge forward...
for example IN SHORT - - when u see bullet tracers in Call of Duty - Modern Warfare 3
Do not place responses in your answers. Just use my method and Translate() the bullet trail forward, since during instantiation it will be facing the center of the screen anyways.
Your answer
Follow this Question
Related Questions
transform.right not working correctly? 0 Answers
How to get Raycast surface normal to align particle system? 1 Answer
Offset raycast by specific amount 1 Answer
Detect if on ground? 1 Answer
RayCast offset rotation 20 degrees 1 Answer