Getting GameObject to come out of the front of a Cube.

Hi guys! I am just messing around a little in Unity working on a project for College.

I am having a small issue with the position of a gameObject called ‘bullet’ form the cube that I am using as the base for the player. The Bullet comes out of the top of the cube and flies into the camera. I want the bullet to come from the front of the camera. Here is a bit of very bad code that I did very wrong! But this code makes the bullet come out of the floor even though the script is on the cube;

using UnityEngine;
using System.Collections;

public class shoot : MonoBehaviour {
	
	public float speed = 60f;
	public GameObject bullet;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		if(Input.GetKeyDown(KeyCode.Space))
		{
		Instantiate(bullet, transform.position, (Quaternion.forward * speed * Time.deltaTime));
	}
}
}

and here is the code that makes the bullet come out of the top of the cube.

sing UnityEngine;
using System.Collections;

public class shoot : MonoBehaviour {
	
	public GameObject bullet;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		if(Input.GetKeyDown(KeyCode.Space))
		{
		Instantiate(bullet, transform.position, transform.rotation);
	}
}
}

not much change but there it is anyway! The Bullet has a Rig Body and is a trigger.

Thanks for any help.

The simplest way to make sure that your bullet comes out out of the camera is to attach your shoot script to the camera (not the cube). Then you need to apply a force to the bullet that is directed towards the cube (assuming that the cube is your target).

Thank for the reply. I think a better way to put the issue is like I did for another forum.

I am making a simple top down shooting game. I am using C# to write out a script for a shooting gun. the bullet works great! But when the GameObject moves around the bullet will continue to go toward the z axis but not the gameObjecs. If that makes sense!

here is the line of code for the BulletsAI

using UnityEngine;
using System.Collections;

public class bulletAI : MonoBehaviour {
	public GameObject player;
	public float speed;

	// Use this for initialization
	void Start () {
		Vector3 newVelocity = Vector3.zero;
		newVelocity.z = speed;
		rigidbody.velocity = newVelocity;
	}
	
	// Update is called once per frame
	void Update () {
		Vector3 newPosition = transform.position;
		newPosition.z += speed * Time.deltaTime;
		player.transform.position = newPosition;
	
	}
}

Here is the Shoot script that is on the player prefab.

using UnityEngine;
using System.Collections;

public class shoot : MonoBehaviour {
	
	public GameObject bullet;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		if(Input.GetKeyDown(KeyCode.Space))
		{
		Instantiate(bullet, transform.position, transform.rotation);
	}
}
}

To make emphasis on what I mean is, that I need the bullet to come from the z axis of the cube at all times. Even when the cube has moved i.e

the cube is at the starting point pointing to the z axis, the player moves around so now the z axis is now facing to the right now. If this happens with the current script the bullets will still shoot to the z axis of the world and not the cube!

Hope that makes sense!

Thanks!

Anyone know? I have been trying for some time now. Although I have added a few new things to the game! Rotate with the mouse is great for it!

so its shooting vertical and you want it horizontal?

tilt its angle so that its horizontal using the angle eulerAngles.x + 90.