- Home /
Problem was solved.
bullets acting weird when spawn
When I spawn a bullet in-game, it sometimes moves to the left or right before going forward. Also, if I spawn one bullet going in one direction and another in a different direction, it seems to yank the other bullet in the direction it's going for a short distance. Here is a link to a video of this happening:
https://drive.google.com/open?id=0B_HzgsC9lP3nS2RkUC1xRkU1Tmc
Here is my bullet spawning script:
using UnityEngine; using System.Collections;
public class BulletController : MonoBehaviour {
public GameObject BulletSpawn;
public GameObject Bullet;
GameObject BulletClone;
public float BulletSpeed;
void Update()
{
if (Input.GetKeyDown("space") == true)
{
BulletClone = (GameObject)Instantiate(Bullet, BulletSpawn.transform.position, BulletSpawn.transform.rotation);
}
Rigidbody rBody = GetComponent<Rigidbody>();
rBody.velocity = BulletClone.transform.forward * BulletSpeed;
}
}
Why are my bullets acting weird?
Not possible. Pressing the space bar only spawns one bullet.
As someone already mentioned they might be colliding with the gun. Can you set their trigger off?
just saw your link to the video... As everyone said, it does look like it is hitting something. OR You or it's adding another force somewhere along the process.
The only part of the player that effects the bullets are the red body, gray shield, and larger blue cylinder.
Answer by FlightOfOne · Jul 19, 2016 at 06:08 PM
All physics stuff should be done in
FixedUpdate()
,It is not good to directly change the velocity of a "Rigidbody", you should be using forces to get desired velocity,
Try this instead:
Instead of Update() use fixed.
void FixedUpdate() { if (Input.GetKeyDown("space") == true) { BulletClone = (GameObject)Instantiate(Bullet, BulletSpawn.transform.position, BulletSpawn.transform.rotation); Rigidbody rBody = GetComponent<Rigidbody>(); rBody.AddRelativeForce(BulletClone.transform.forward * BulletSpeed,ForceMode.Impulse); //rBody.AddRelativeForce(BulletClone.transform.forward * BulletSpeed, ForceMode.VelocityChange); //<---Note ForceMode.VelocityChange, try this mode also, it ignores the mass } }
You should disable everything, I mean everything. better yet, start a new scene. Just 1 Empty object, your bullet, and obviously the scripts(s). Then try again.
Also, make sure the bullet is not a child of anything.
Also be sure that you are not applying force every frame. $$anonymous$$ake sure the portion of code that applies force is inside the if(Input) statement.
One last thing, do you have any weird scales set, of Collider offsets?
Answer by mnmwert · Jul 19, 2016 at 09:21 PM
here is a script that should make the bullets fire right. but bring your spawn point out a little more.
using UnityEngine;
using System.Collections;
public class shotSpeed : MonoBehaviour {
public float speed;
public Vector3 target;
public Transform target1;
// Use this for initialization
void Start ()
{
target = target1.transform.position;
}
// Update is called once per frame
void Update ()
{
var delta = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, target, delta); // move towards //target
if(transform.position == target)
{
Destroy(gameObject);
}
}
}
Follow this Question
Related Questions
Bullet Not Working 3 Answers
trouble making a fast projectile 2 Answers
Why are these object passing through each other? 1 Answer
Add velocity relative to ground? 1 Answer