- Home /
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!
$$anonymous$$ake sure that "transform" is located at the gun muzzle.
$$anonymous$$y 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
Answer by superluigi · Jan 18, 2015 at 10:46 PM
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
ok that's weird. I never apply root motion from animations to my characters, so i can't necessarily say if it leaves your transform alone while the character moves, but I doubt that's the case. There's absolutely no reason for the sphere spawn point to jump to the top of your character unless a script is telling it to do so. Check every code you have that references the sphere spawn point and look for the line where you're messing with it's transform.position. Also before you do that, real quickly turn off apply root motion on your character so that the animation doesn't move it's position, hit play and check if the sphere still jumps to the top of your character. Also try deactivating your animator if possible (might not be possible depending on your code) and check if it still does that. finally, while in play mode, in the scene view select and manually drag your character and check if the sphere follows your character
Your answer
Follow this Question
Related Questions
Overlapsphere isn't working as expected. 0 Answers
Best way to move between maps 1 Answer
NullRef error 1 Answer
Instantiate Problem 2 Answers