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: - YouTube
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
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).
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.