Question by
Josh5hmo1 · May 10, 2018 at 09:29 PM ·
2dscript.rigidbody2d
Can't change velocity on instantiated object
public class PlusShoot : MonoBehaviour {
public GameObject bullet;
int timer;
// Use this for initialization
void Start () {
timer = 0;
}
// Update is called once per frame
void Update () {
if (GameObject.FindGameObjectWithTag("Enemies") != null) {
if (timer == 50) {
Instantiate (bullet, transform.position, transform.rotation);
timer = 0;
}
timer++;
}
}
void Shoot(){
var obj = (GameObject)Instantiate (bullet, transform.position, transform.rotation);
obj.GetComponent<Rigidbody2D> ().velocity = new Vector2 (2.5f, -1.7f);
//Instantiate (bullet, transform.position, transform.rotation);
//Instantiate (bullet, transform.position, transform.rotation);
//Instantiate (bullet, transform.position, transform.rotation);
}
}
So the bullet prefab that is getting spawned in has a rigidbody2d attached to it and still nothing happens. Anyone have any idea as to why?
Comment
Answer by tormentoarmagedoom · May 11, 2018 at 07:52 AM
Good day.
can you post the inspector of the bullet prefab? IS posible you have marked any Restrictions in some Axis? And you should have "Use Gravity off" and "Is Kinematic on"
And, You have 2 places where the bullet is spawned, and thisone is not modifying its velocity:
if (GameObject.FindGameObjectWithTag("Enemies") != null) {
if (timer == 50) {
Instantiate (bullet, transform.position, transform.rotation);
timer = 0;
}
timer++;
}
Have the bullet any script that is modifying the position or the rigidbody?
Give feedback please!
Bye! :D