When I set spawn point to a transform, when I play it changes

When I play my transform changes and I have set the prefab to the same position to be sure, I am trying to make my bullet fire out of my gun barrel here is the script

using UnityEngine;
using System.Collections;

public class Shooting : MonoBehaviour {

public Rigidbody BulletPrefab;
public float speed = 100f;

// Update is called once per frame
void Update () {
	if (Input.GetMouseButtonDown(0)){
	Rigidbody BulletInstance;
	BulletInstance = Instantiate(BulletPrefab, transform.position, transform.rotation) as Rigidbody;
	BulletInstance.AddForce(transform.forward * speed);
		}

	


}

}

thanks!

Make sure that "transform" is located at the gun muzzle.

what exactly is the problem you are having?

My bullets spawn above my player even if I move the transform of my bullet spawn point

and my spawn is placed at the muzzle but it just spawn over my player

1 Answer

1

If I’m not mistaken, the final line of your code has it adding force to your bullet every Update and at the transform.forward of your gun and not the bullet. You should always handle physics such as addforce in fixedUpdate and not Update, and your line also means that when your player turns the bullet is gonna turn with it (because every frame it will = the forward of your gun instead of the forward of the bullet)

are you shooting in first person view? If that's the case then when you look up, you might not see the bullet if your script isn't attached to the camera or something with the cameras rotation. Let me know so i can help you further and explain exactly what i mean

no im in third and it spawns above my player wherever I place it, thanks for answering though!

ook so is your spawn point an empty game object? the fix is really quite simple. Separate your game view from your scene view, and make sure that you don't maximize your game view when you hit play. Now select your spawn point in the hierarchy and check if it's above your player when you hit play, also check if it moves with your player when he walks.

my spawn point is a sphere and I can move it to my barrel but it will just move back over my player when I hit play, also it moves with the player but it will move further away every second, I think it might be my animation because it makes my soldier move slightly every frame, it also wont shoot forward in front of my spawn point like it should be