Hi, I have a moving crosshair and I want the bullets to shoot wherever the crosshair is aimed. A little more specific; the crosshair isn’t locked in the middle of the screen. It moves around as in the video game Lost Planet. The bullets just fire straight.
To be honest I’d like a basic script if possible that allows me to problem solve to make it work for me…I don’t know if that’s asking too much…
Create your bullet and set its direction to ( crosshair.position - bulletSource.position ) and you’ll be good to go.
For physical bullets,
Bullet.js
var myDirection : Vector3;
var mySpeed : float; // have to set this somewhere
function SetDirection( d : Vector3 ) {
myDirection = d;
}
function Update() {
transform.position += myDirection * mySpeed;
}
For raycast bullets, use a ray with the direction.
Okay so I think I got the jist of that code. BUT, it’s not working and I think I know why. Firstly if this is wrong do tell me. But I think it’s because the “crosshair” itself is a GUI Texture and associating the crosshair.position with that isn’t seeing it as a game object to follow. And the actual code that moves this crosshair is inside the spaceship object. (I did a lot of different things to that code and tried several different methods.)
Also, I’m curious – Is there a way to have the Z axis of the game object that is shooting these bullets follow where the cursor is aimed? Example - A gun barrel rotating. If so I would think that would simplify it some.
I have a few more ideas though. I’ll continue to try and figure it out.
I just realized how vague I’m being.
The “crosshair” turns the ship. When you move the mouse left, right, up, down and so on, the ship moves accordingly. If there is anything else that seems unclear just say so.
I’ve spent a good hour + on this. I feel like I’m getting close but I’d say it’s time to throw in the towel and ask for more specific direction. haha
So I’m definitely asking for more specific instructions. haha
// Get worldspace representation of mouse position
var ray : Ray = Camera.ScreenPointToRay( Vector3(mouseX,mouseY,0) );
// Convert ray to local-to-ship location ("far away in the direction I'm pointing"
// the ship, or at least the bullet source(s), should probably be pointed towards this point as well for visual authenticity and whatnot
var pointToShootTowards : Vector3 = ray.origin + ray.direction * /*some number*/ 100;
// Create a bullet
var bullet : Bullet = Instantiate( bulletObject );
// Move the bullet to where the bullets shoot out from
// e.g. the gatling gun mounted on the beak
bullet.transform.position = ship.positionToShootFrom;
// Rotate the bullet's Z axis to look at LookDirection and attempt to keep its Y axis pointed towards World-Up
var lookDirection : Vector3 = pointToShootTowards - bullet.transform.position;
bullet.transform.rotation = Quaternion.LookRotation( lookDirection, Vector3.up );
Yeah, the above code is the general idea - though more detail on your game-play would be nice as there are a couple different way’s of utilizing it depending on how your game works.
For example, it probably won’t work that well if your doing a top-down game, but should be fine for a FPS/TPS with rotating turrets.
The game type is a 3rd perspective 3D space ship game. I’ve simplified the controls quite a bit and gave the player much more control over it. So now the cursor or crosshair is all on its own. It’s basically now just the hidden cursor represented by the GUI Texture.
Does that help or am I still being too nonspecific?
Well, I ran into a major snag. The ship can go forward and turn left and right but can’t go up and down. So I guess I’m putting the cursor moving the ship code back in and back to square one. haha Sorry about that. Trial and error.