Hi all,
i’ve got a problem with the shooting script
I don’t get any errors, here is the script:
void Shoot()
{
Rigidbody prj = Instantiate (proj,FireNode.transform.position,FireNode.rotation) as Rigidbody;
Debug.Log(prj.transform.position);
proj.velocity = transform.TransformDirection(new Vector3(0,0,20));
}
What’s the problem? the problem is that the projectile will spawn at coords (0,0,0)
mean the firenode position is in a prefab at (179.73,13.81,457.04)
How can i solve this?
When Instantiating a bullet, i usually have the bulletmovement in another script,
I just tried this out with your method and it worked if you want to shoot forward in the Z
using UnityEngine;
using System.Collections;
public class SpawnBullet : MonoBehaviour {
// Use this for initialization
public GameObject bullet;
public Transform bulletTransform;
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if (Input.GetMouseButton(0))
{
Shoot();
}
}
void Shoot()
{
Rigidbody prj = (Rigidbody)Instantiate(bullet, bulletTransform.position, bulletTransform.rotation);
}
}
then in the bulletmovement script which is attatched to the bullet
using UnityEngine;
using System.Collections;
public class bulletMovement: MonoBehaviour {
// Use this for initialization
public float force;
void Start ()
{
}
// Update is called once per frame
void Update ()
{
gameObject.GetComponent<Rigidbody>().velocity = transform.TransformDirection(Vector3.forward * force);
}
}
However, If you want to shoot in other directions, this wouldn’t work
I have to shoot to the enemies that are moving trought the map
Anyway i found where is the problem.
If i disable the collision of the bullet,it will spawn correctly in the firenode position.
But if i enable to on the collision it will spawn somewhere in the world.
How can i fix this?
Are you firing towards a mouse position? or do you want to shoot forward at any rotation?
I can shoot correctly to the enemies just adding a force to the rigidbody but…the core of the issue isn’t this.
look:
is the FireNode position placed within another collider? try off set it by a little
No, it isn’t the fire node is in a mesh prefab without collisions