- Home /
Question by
TheManFromMars · May 05 at 02:30 AM ·
fps controllerprojectiles
(FPS) How do you fire in the direction the player is facing?
I'm trying to fire a projectile in the direction my character is facing yet it only works aiming from 0 to 180 degrees, other than that it won't fire properly in the direction I'm facing. Can someone look at my code and tell me what I'm doing wrong? I've looked up solutions but can't find one. Thanks in advance for any help.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DoomPlayerController : MonoBehaviour
{
// Properties
public int speed = 10;
public int turnSpeed = 5;
private CharacterController cc;
public MoveBullet bullet;
// Start is called before the first frame update
void Start()
{
cc = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
Vector3 position = cc.transform.position;
var direction = transform.rotation;
if(Input.GetKey(KeyCode.W))
{
cc.Move(transform.forward * speed * Time.deltaTime);
}
if(Input.GetKey(KeyCode.S))
{
cc.Move(-transform.forward * speed * Time.deltaTime);
}
if(Input.GetKey(KeyCode.A))
{
transform.Rotate(Vector3.down * turnSpeed * Time.deltaTime);
}
if(Input.GetKey(KeyCode.D))
{
transform.Rotate(Vector3.up * turnSpeed * Time.deltaTime);
}
if(Input.GetKeyDown(KeyCode.Space))
{
MoveBullet clone = Instantiate(bullet, position, direction);
var body = clone.GetComponent<Rigidbody>();
body.AddForce(transform.forward * 10000);
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613060203im_/https://answers.unity.com/themes/thub/images/avi.jpg)