I’m creating a FPS Game. I have a firepoint gameobject which is the child of the Player. With firepoint as a reference, I’m firing bullets. Following is my code,
public class PlayerController : MonoBehaviour
{
public float moveSpeed, gravityModifier, jumpPower, runSpeed =12f;
public CharacterController charCon;
private Vector3 moveInput;
public Transform camTrans;
public float mouseSensitivity;
public bool invertX;
public bool invertY;
private bool canJump, canDoubleJump;
public Transform groundCheckPoint;
public LayerMask whatIsGround;
public Animator anim;
public GameObject bullet;
public Transform firePoint;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
// store y velocity
float yStore = moveInput.y;
Vector3 vertMove = transform.forward * Input.GetAxis("Vertical");
Vector3 horiMove = transform.right * Input.GetAxis("Horizontal");
moveInput = horiMove + vertMove ;
moveInput.Normalize();
if (Input.GetKey(KeyCode.LeftShift))
{
moveInput = moveInput * runSpeed;
}
else
{
moveInput = moveInput * moveSpeed;
}
moveInput.y = yStore;
moveInput.y += Physics.gravity.y * gravityModifier * Time.deltaTime;
if (charCon.isGrounded)
{
moveInput.y = Physics.gravity.y * gravityModifier * Time.deltaTime;
}
canJump = Physics.OverlapSphere(groundCheckPoint.position, 0.25f, whatIsGround).Length> 0;
if (canJump)
{
canDoubleJump = false;
}
//handle jumping
if (Input.GetKeyDown(KeyCode.Space) && canJump)
{
moveInput.y = jumpPower;
canDoubleJump = true;
}
if (Input.GetKeyDown(KeyCode.Space) && canDoubleJump)
{
moveInput.y = jumpPower;
canDoubleJump = false;
}
charCon.Move(moveInput* Time.deltaTime);
//Control Camera Rotation
Vector2 mouseInput = new Vector2(Input.GetAxisRaw("Mouse X"),Input.GetAxisRaw("Mouse Y")) * mouseSensitivity;
if (invertX)
{
mouseInput.x = -mouseInput.x;
}
if (invertY)
{
mouseInput.y = -mouseInput.y;
}
transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y + mouseInput.x,
transform.rotation.eulerAngles.z);
camTrans.rotation = Quaternion.Euler(camTrans.rotation.eulerAngles + new Vector3(-mouseInput.y,0f,0f));
//Handle Shooting
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
if (Physics.Raycast(camTrans.position, camTrans.forward, out hit,50f))
{
if (Vector3.Distance(camTrans.position, hit.point) > 2f)
{
firePoint.LookAt(hit.point);
}
}
else
{
firePoint.LookAt(camTrans.position + (camTrans.forward * 30f));
}
Instantiate(bullet, firePoint.transform.position,firePoint.transform.rotation);
}
anim.SetFloat("moveSpeed",moveInput.magnitude);
anim.SetBool("onGround",canJump);
}
}
The problem is that the bullet is always being fired from the same position though firepoint is the child of the Player. What am I missing?
UPDATE:
Following is the code that is attached to the bullet prefab.
public class BulletController : MonoBehaviour
{
public float moveSpeed, lifeTime;
public Rigidbody theRB;
public GameObject impactEffect;
// Update is called once per frame
void Update()
{
theRB.velocity = Vector3.forward * moveSpeed;
lifeTime -= Time.deltaTime;
if (lifeTime <= 0)
{
Destroy(gameObject);
}
}
private void OnTriggerEnter(Collider other)
{
Destroy(gameObject);
Instantiate(impactEffect, transform.position + (transform.forward * (-moveSpeed * Time.deltaTime)), transform.rotation);
}
}
Following is the code that is controls the camera,
public class CameraController : MonoBehaviour
{
public Transform target;
void LateUpdate()
{
transform.position = target.position;
transform.rotation = target.rotation;
}
}
The problem is that the bullet is always being fired from the same position though firepoint is the child of the Player. What am I missing?
BulletController is attached to the bullet prefab. CameraController is attached to the MainCamera. PlayerController is attached to the Player game object.