- Home /
Camera following Bullet
Hi, im making a FPS game, but when i shoot i want the camera starts following the object, like bullet time in matrix, i've tried a lot of "answers" with similar topics at this site, but i cant get it to work, here is the code where i create the bullet:
var bullet:GameObject;
var sound1: AudioClip;
function Start () {
}
function Update () {
if(Input.GetButtonDown("Fire1")){
audio.PlayOneShot(sound1);
var bulletInstance:GameObject = Instantiate(bullet,transform.position,Quaternion.identity);
Physics.IgnoreCollision(transform.root.collider , bulletInstance.collider);
bulletInstance.rigidbody.AddForce(
Camera.mainCamera.transform.TransformDirection(Vector3(0,0,100))
,ForceMode.Impulse);
}
}
thanks!
Answer by Griffo · Mar 09, 2013 at 08:01 PM
The way I do it is add another camera to the scene then child the bullet model you have to it then save the camera as the bullet prefab ready to Instantiate as your bulletInstance.
Then put 2 new variables at the top of your script -
var Camera2 : Camera;
private var Camera1 : Camera;
Drop your bullet prefab into the Camera2 slot in the Inspector, then in the Start function add -
Camera1 = Camera.main;
Then when you fire your bullet add -
Camera1.enabled = false;
Camera2.enabled = true;
Then add a script to your bullet prefab with the reverse -
Camera1.enabled = true;
Camera2.enabled = false;
Triggered on collision of the bullet.
Hope this helps.
Hi Griffo, thanks for the answer, i did what you said and it worked fine, however i still have some issues:
*The main camera its a child from the FPS character, so how do i activate it again from the bullet script?
*The camera is with the bullet all the time, im wondering if there's something i can do for the camera to be behind the bullet.
Thank you very much!
Oh, i manage to switch cameras, so thats no longer a problem, i still sarching about how to put the camera behind the bullet, thanks
To put the camera behind the bullet just drop the prefab into the scene then if you did as I said you should have the camera with the bullet as its child, just click on the bullet and move it slightly forward until its where you want it in view of the camera, then delete the original prefab and drop this one into the prefab folder as the new one, then the next time you fire you should see the bullet just in front of the camera.
If my original answer was correct please mark as answered, the tick, thank you.
Thank you so much for your time and your answers, now it works perfectly!
Your answer
Follow this Question
Related Questions
Super Monkey Ball type Camera? 2 Answers
How to make your character move? 7 Answers
Fire bullet to mouse position in 3d space 1 Answer
Follow an object with camera -1 Answers