Hi,
First time question. I'm working on a game for my unity final. The premise is you're in a ship shooting at things as you travel down a tube.
The ship is always moving forward, increasing in speed as it goes along. The player uses wsad keys to move the ship around obsticles and things shooting at it, and then uses the mouse as a reticle to aim and shoot (It's red and to the left of the hole) My problem? I can't find a way to shoot from the ship (accurately).
I've looked at several different paths, but mostly when it comes to shooting, everyone and their mother suggests the unity first person shooter method, which doesn't work in this instance, because I'm not shooting from the camera. When the normal shooting script of using the physics engine is used with rigid body bullets, instantiated from the ship, the bullets don't go where they need to. They look like they do, but instead of shooting at the proper angle from the ship, they shoot at the angle from the camera, making their aim off by a significant amount
I've gotten close using non-physics engine scripting, but I've hit a stop gap. Using a raycast in conjunction with ScreenPointToRay, I can get the point I need it to go to, but I can't make it travel to that point, I can only make it instantiate at that point. I've tried cloning it, I've tried giving the functions to the prefab I'm using, along with several other methods and I can't seem to get anything to work. And then after that, I still have to worry that it's not going to hit the ship when it sees it as a target.
Here's my code named Crosshair. The comments show the methods that I've tried and their various failings. It's a bit messy at current, but things really aren't being bothered. This is normally connected to the camera, but I also tried connecting it to a plain infront of the ship that I made invisible.
var reticle : Texture2D;
Screen.showCursor = false;
var mousePos;
var mx: float;
var my: float;
var particle : GameObject;
var hit: RaycastHit;
var projectile : Rigidbody;
var beam : Laserbeam;
function OnGUI () {
mousePos = Event.current.mousePosition;
GUI.DrawTexture( Rect(mousePos.x-(reticle.width/2), mousePos.y-(reticle.height/2), reticle.width, reticle.height), reticle);
}
function Update(){
//When fire one is fired, if it encounters a solid place,, place a particle, right now a cube
if (Input.GetButtonDown ("Fire1")) {
// Construct a ray from the current mouse coordinates
var ray2 = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray2, hit)) {
print(hit.point);
print(ray2);
// Create a particle if hit directly where it needs to be - works, but instantanious
//Instantiate (particle, hit.point, transform.rotation);
//Try to move the particle there - failure
//var b= Instantiate (particle, Vector3(0, -5, 0), transform.rotation);
//b.transform.Translate(hit.point);
//attempting example from Website - Rigidbody proving too hard to aim
// var clone : Rigidbody;
// clone = Instantiate(projectile, transform.position, transform.rotation);
// Give the cloned object an initial velocity along the current
// object's Z axis
//clone.velocity = transform.TransformDirection (hit.point);
//clone.mass = 0.5;
//Attempt to instante object and then make it travel to target. Failure
var las : Laserbeam;
las = Instantiate(beam, transform.position, transform.rotation);
//Attempt to hit hit point- failure
//las.fired2(hit.point);
Here is the move script I'm using while testing it. The other versions I've made up have actual acceleration and breaking involved for the ship
function Update () {
//print("time=" + Time.time + "target=" + target);
var craft = GameObject.FindWithTag("PlayerCraft");
//If you detect the first mouse button go up, aka, click
if( Input.GetAxis("Horizontal")==0 && Input.GetAxis("Vertical")==0){
target= Vector3(0, transform.position.y, 0);
if(smooth){
//Vector3.distance determines the amount to move
var dist = Vector3.Distance(transform.position, target);
//Mathf.smoothStep will smooth based on startTime and steps
var Step = Mathf.SmoothStep(0, dist, (Time.time - startTime)/steps);
//Vector3.MoveTowards will moe it towards the place
transform.position = Vector3.Lerp(transform.position, target, Step);
}
else{
transform.position = target;
}
transform.position.y--;
}
else{
transform.Translate(Input.GetAxis("Horizontal"), -1, Input.GetAxis("Vertical") );
transform.position.x=Mathf.Clamp(transform.position.x, -10.0, 10.0);
transform.position.z=Mathf.Clamp(transform.position.z, -10.0, 10.0);
startTime=Time.time;
if(Input.GetAxis("Horizontal")>0){
craft.transform.rotate
}
}
}
Any help would be vastly appreciated in this. Thank you.