Bullets are firing in the wrong direction.
I'm back at it again with another problem. This time, it's about bullets and raycasting. My bullets are only firing in one direction and I have no idea as to how to make it relative to the player's rotation. This seems like a pretty easy thing to fix, but I just can't come up with an idea as to how to fix it. Here's the code, props to anyone who can lend me a hand.
`using UnityEngine;
using System.Collections;
public class BulletMovement : MonoBehaviour
{
public int speed = 20;
float timer = 20f;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.Translate(Vector3.back * 20 * Time.deltaTime);
if (Camera.main.WorldToViewportPoint(this.transform.position).y > 1)
{
Destroy(this.gameObject);
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Enemy"))
{
Destroy(other.gameObject);
Destroy(this.gameObject);
}
}
}
`
By the way, the code in the original post was for the bullet, not the shooting script. If you want the shooting script, I have it right here.
using UnityEngine;
using System.Collections;
public class shooting : $$anonymous$$onoBehaviour {
public GameObject bulletAmmo;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
RaycastHit hit;
Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 50f);
GameObject bullet = Instantiate(bulletAmmo, transform.position, Quaternion.identity) as GameObject;
bullet.transform.LookAt(hit.point);
}
}
}
Answer by doublemax · Oct 17, 2016 at 09:08 PM
transform.Translate(Vector3.back * 20 * Time.deltaTime);
Try this instead:
transform.Translate(transform.forward * 20 * Time.deltaTime);
The same? Nothing changed?
Can you describe the bullet path? Where does it go and does it change at all when you aim in different directions?
It only goes in one direction. If you're facing south, for example, it'll fire straight, but if you're facing to the right then it'll fire to the left. It's only firing southbound. The direction of the bullet is not relative to the player, but relative to the set direction. I was looking for a solution that would track the player's direction and fire in said direction.
Your answer
Follow this Question
Related Questions
How does one continuously move an object forward without using velocity? 1 Answer
How do I define the direction and speed of a projectile separately? (JS) 0 Answers
Bullet clones will not be destroyed. 2 Answers
Instantiate vs Instantiate as gameobject 1 Answer
Aiming and shooting to the same point in third person 1 Answer