- Home /
Want Ships & Bullets to transform relative to Camera.
I'm making a 2.5D space shooter game much like R-Type and G-darius. Like G-Darius, I want the camera to fly around the level (all cinematic and stuff) with all of the on-screen action taking place relative to the camera (the plane of gameplay moves along with the camera.
If you take a look at this: http://www.youtube.com/watch?v=YUBbJu5NurE you can see that regardless of how fast the camera rotates/moves around the environment, the bullets are always relative to the camera in speed and gravity etc.
So far I make my bullets move like this (on creation in the bullet spawn object):
var cloneLaser1 : GameObject = Instantiate(laser,shootSpwnPos1.transform.position,shootSpwnPos1.transform.rotation) as GameObject;
cloneLaser1.rigidbody.velocity = transform.TransformDirection(Vector3.down*initialLaserSpeed);
So what I need is a local movement logic script, to tell me how I should make everything have velocity, but always being a child of the main camera so it follows it around everywhere. Any ideas? I want to avoid the parallax scrolling method and I want to make the game quite dynamic camera movement wise like you see in the video.
To clarify, it's as if the action is always static on the screen, and there's just a video playing in the background, but I don't want a video playing, I want an actual 3D environment passing the player by. I tried making bullets go along the main camera's rotation horizontal axis, but if the camera rotates, then the bullet wouldn't travel straight along the screen and veer into the background somewhere :S
I'm confused here. To me, in the video all the bullets are relative to the ship, not the camera. The camera just follows the ship. Or is your setup different?
The camera is flying through the level, and it's as if all the assets are following it around the level. Currently my ship is the child of the camera, which means it can move around in relation to the camera axis fine. When I shoot however, if the camera is rotating around, the bullet will start going left, but then the camera rotates and the bullet starts falling further away, and will miss any enemies (it's not travelling in a straight line like in the video).
Because the world orientation of the ship is changing along with the camera's, the ship is rotating fine, I want to know how I can get a travelling bullet to do the same, so that once it's fired, it still rotates around with the camera regardless of how it's moving.
All of what I want can be achieved by just playing a movie on a plane in the background, but I want to do it properly.
I still don't have a solid understanding of the desired behavior. One thing you can do (which surprised me the first time I did it unintentionally) is to make the projectiles a child of the camera. Just do this before setting the velocity:
cloneLaser1.transform.parent = Camera.main.transform;
Another possibility is to give the projectiles an initial velocity that is a combination of the velocity of the camera and the velocity you want in the projectile.
I think robertu gave the simplest thing to try, making the projectile a child of the camera is probably the quickest way to achieve what I want. Then I can give it velocity and it will always snap to the camera's movement. Something tells me velocity will be a world thing though, and won't work locally :S so I'll have to test it out first. I'll try this first though!
Answer by MrThreepwood · Jun 15, 2013 at 04:20 AM
The game you're talking about pretty clearly just uses a background video. If you don't want to do that (and thus allow for an actual 3d environment for you to fly through) then you're going to need scripts that control the speed/acceleration of your projectiles. I would guess that you're currently just giving them an initial speed and letting them fly.
Instead, create a script that handles there movement, and leave their speeds/accelerations all relative to the camera. It would be easiest to simply have a specific velocity set for each projectile. As for gravity, if you want it to influence your projectiles (why?) you'll have to handle the acceleration relative to the ship as well.
Of course, I'm not sure that you can set an objects speed in a direction relative to something else, so there may be some math involved for you.
An easy way to do it, since your ship is already doing what you want it to as the camera turns, is to just take the vector of your ship.forward (assuming that your ship model is oriented properly)and set your bullets speed along the same vector. (assuming that your bullet just travels perfectly horizontal of course).
It will be travelling along the ship vector perfectly horizontally, you're correct. If you think of a pair of rails just co$$anonymous$$g out of the ship's nose, and train going back and forth on a perfectly straight line relative to the ship, regardless of how it's rotating, that's the effect I want, but ins$$anonymous$$d of the rails there, just think of them existing only in code, and the lasers will only travel one way and not come back (I make them destroy after 4 seconds anyway).
Answer by $$anonymous$$ · Jun 16, 2013 at 11:19 PM
Ok guys I actually managed to work it out based on robertu's initial answer.
When I create anything (lasers, powerups, the works) I set the transform.parent to the main camera which now makes it a child of the main camera.
I then put in this code for making the lasers move on spawn, in local space relative to the parent constantly every frame in the Update() function:
transform.localPosition = Vector3(transform.localPosition.x + (-bulletSpeed*Time.deltaTime), transform.localPosition.y, transform.localPosition.z);
This thus allows me to spawn anything, make it a child of the main camera, and then move it constantly every frame in relation to delta time, across the screen. The beauty of this of course is that I can then add another camera to do the cinematic stuff, and won't need to change parents and children etc., I can just set it as a secondary camera purely for cosmetic stuff.