- Home /
The question is answered, right answer was accepted
GameObject Instantiated in Wrong Position
Hi,
I'm making a gun system, and when I click I instantiate a bullet with the player position, the problem is that:
//BulletScript attached to the bullet Gameobject
using UnityEngine;
using System.Collections;
public class BulletScript : MonoBehaviour {
// Use this for initialization
void Start () {
float x = GameObject.Find("Player").transform.FindChild("mainCam").transform.position.x+3;
float y = GameObject.Find("Player").transform.FindChild("mainCam").transform.position.y;
float z = GameObject.Find("Player").transform.FindChild("mainCam").transform.position.z+3;
transform.position = new Vector3(x, y, z);
}
// Update is called once per frame
void Update () {
transform.rigidbody.AddForce(transform.eulerAngles*10);
}
}
And the second script:
//GunScript attached to the Inventory Item (it's a MP5)
using UnityEngine;
using System.Collections;
public class GunScript : MonoBehaviour {
GameObject bullet;
// Use this for initialization
void Start () {
if(Input.GetMouseButtonDown(0)) { //This is weird because I have to use it in the Start void, and not in the Update
bullet = GameObject.Instantiate(Resources.Load("Models/Items/Bullets/bala"), GameObject.Find("Player").transform.FindChild("mainCam").transform.position, GameObject.Find("Player").transform.FindChild("mainCam").transform.rotation) as GameObject;
}
}
// Update is called once per frame
void Update () {
}
}
How can I solve that?
Thanks in advance. Bye.
Could you show us some of the code? There are a number of reasons the bullet could be getting placed incorrectly.
Comment out bulletscript Update() then check bullet's position (as you are applying force on it the moment it instantiated. Fire bullet only when fire is clicked.)
Answer by z3nth10n · Mar 16, 2014 at 06:02 PM
Hi,
I solved it sometime ago, but It's only a reminder, don't put anytime a rigidbody with a mesh collider together...
Bye.
Follow this Question
Related Questions
GameObject change Position after game started 1 Answer
Instantiate as a child at position 2 Answers
How to implement a system for "Road-Construction"? 1 Answer
Instantiate a GameObject at a position specific to an element that was found in a text 1 Answer
Adjacent Placement of Objects according to varying height 0 Answers