I have made a cube and I want a bullet to be fired from the front of this cube from the direction it is facing , but can’t figure out how to do this ?
I have been giving this problem a lot of thought and I think that I somehow need to get the rotation of the cube and pass this to the script for my bullet, but this is done I simply can’t figure out ?
My bullet script:
using UnityEngine;
using System.Collections;
public class Bullet : MonoBehaviour {
private Transform bullet;
public int bulletSpeed;
// Use this for initialization
void Start () {
bulletSpeed = 5;
bullet = transform;
}
// Update is called once per frame
void Update () {
bullet.Translate (Vector3.right * bulletSpeed * Time.deltaTime);
Destroy(gameObject,2.0f); //Destroys the bullet after 2 seconds
}
}
Would anyone be so kind and point me in the right direction.
That’s the exact problem I have which I can’t figure out how to solve !
using UnityEngine;
using System.Collections;
public class TankMovement : MonoBehaviour {
public float playerSpeed;
public float playerTurnSpeed;
private Transform myPlayerObject;
public GameObject bulletPrefab; //links the bulletPrefab to this script (I think !)
// Use this for initialization
void Start () {
playerSpeed = 5.0f;
playerTurnSpeed = 20.0f;
myPlayerObject = transform;
myPlayerObject.position = new Vector3(2,1,2);
}
// Update is called once per frame
void Update () {
myPlayerObject.Rotate(Vector3.up * Input.GetAxis("Horizontal") * playerTurnSpeed * Time.deltaTime);
myPlayerObject.Translate(Vector3.right * Input.GetAxis("Vertical") * playerSpeed * Time.deltaTime);
if(Input.GetKeyDown(KeyCode.Space)){
Vector3 position = new Vector3(myPlayerObject.position.x, myPlayerObject.position.y +1, myPlayerObject.position.z);
//This will fire the lazer - Instantiate will create a new clone of the
//ProjectileFab once the spacebar is pressed.
Instantiate(bulletPrefab, position, Quaternion.identity);
}
}
}
I have been looking at this for hours trying to decipher and dissect what they mean, however, the closest I have come to in terms of a solution is this:
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public Rigidbody projectile;
void Update() {
if (Input.GetButtonDown("Fire1")) {
Rigidbody clone;
clone = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
clone.velocity = transform.TransformDirection(Vector3.forward * 10);
}
}
}
However, they use a Rigidbody and I’m not sure if need this as I don’t, to the best of my knowledge, use any physics.
if(Input.GetKeyDown(KeyCode.Space))
{
Vector3 position = new Vector3(myPlayerObject.position.x, myPlayerObject.position.y +1, myPlayerObject.position.z);
//This will fire the lazer - Instantiate will create a new clone of the
//ProjectileFab once the spacebar is pressed.
Instantiate(bulletPrefab, position, Quaternion.identity);
}
you’re using Quaternion.identity… if you want the rotation of the object this script is attached to, you need transform.rotation.
If you haven’t got onto rigidbodies just yet, just attach a script to the bulletPrefab with a simple
function Update()
{
// Move the object forward at 1 unit/second.
transform.Translate(transform.forward * Time.deltaTime);
}
I created two guns each with their own empty object in front of the weapon. I disabled the one where I’m not facing in that direction. I use a check for geyKeyDown to determine which way my cube is facing.
if(GetKeyDown(KeyCode.A))//Left face
{
gun2.SetActive(true);
gun1.SetActive(false);
}
if(GetKeyDown(KeyCode.D)//Right face
{
gun2.SetActive(false);
gun1.SetActive(true);
}
This worked everytime I’ve used it. Even though right now I’m having a problem where its not firing from the empty(bullet spawns) but rather it’s firing on the Z-Axis directly from the player. Pissing me off greatly lol but you can try this method and see if it works for you.
Edit - I’m using 2D mode so might need more code in a 3D environment.