- Home /
Add an aim to a space ship simulator
Hi All! I have a big problem: i need to add an aim that really point to objects in a 3d space attached to my spaceship. The scene is composed by:
GameObject "SpaceShip" with attached script "SchipController";
Its child "SphipMesh;
another child "Shooter" with the script "Shooting" attached;
And the main camera with the script "SmoothFollow" attached with target set to SpaceShip.
Here are the scripts: "ShipController" (JS):
var turnspeed = 5.0;
var speed = 5.0;
private var trueSpeed = 0.0;
var strafeSpeed = 5.0;
function Update () {
var roll = Input.GetAxis ("Roll");
var pitch = Input.GetAxis ("Pitch");
var yaw = Input.GetAxis ("Yaw");
var strafe = Vector3 (Input.GetAxis ("Horizontal") * strafeSpeed * Time.deltaTime, Input.GetAxis ("Vertical") * strafeSpeed * Time.deltaTime, 0);
var power = Input.GetAxis ("Power");
//Truespeed controls
if (trueSpeed < 10 && trueSpeed > -3) {
trueSpeed += power;
}
if (trueSpeed > 10) {
trueSpeed = 9.99;
}
if (trueSpeed < -3) {
trueSpeed = -2.99;
}
if (Input.GetKey ("backspace")) {
trueSpeed = 0;
}
rigidbody.AddRelativeTorque (pitch * turnspeed * Time.deltaTime, yaw * turnspeed * Time.deltaTime, roll * turnspeed * Time.deltaTime);
rigidbody.AddRelativeForce (0, 0, trueSpeed * speed * Time.deltaTime);
rigidbody.AddRelativeForce (strafe);
}
"Shooting" (C#):
using UnityEngine;
using System.Collections;
public class Shooting : MonoBehaviour {
public Rigidbody plasma;
public Transform plasmaFlash;
public AudioClip plasmaShootFx;
void Update () {
if (Input.GetKeyDown (KeyCode.Space)) {
Rigidbody clone;
clone = Instantiate (plasma, transform.position, Quaternion.Euler (0, 90, 90)) as Rigidbody;
clone.velocity = transform.TransformDirection (Vector3.forward * 800);
Instantiate (plasmaFlash, transform.position, transform.rotation);
audio.PlayOneShot (plasmaShootFx);
Destroy (clone.transform.gameObject, 2);
}
}
}
And the SmoothFollow (JS):
var target : Transform;
var distance = 3.0;
var height = 3.0;
var damping = 5.0;
var smoothRotation = true;
var rotationDamping = 10.0;
function FixedUpdate () {
var wantedPosition = target.TransformPoint(0, height, -distance);
transform.position = Vector3.Lerp (transform.position, wantedPosition, Time.deltaTime * damping);
if (smoothRotation) {
var wantedRotation = Quaternion.LookRotation(target.position - transform.position, target.up);
transform.rotation = Quaternion.Slerp (transform.rotation, wantedRotation, Time.deltaTime * rotationDamping);
}
else transform.LookAt (target, target.up);
}
Now, how i can position the aim plane in the way of real point to asteroids (directly forward the ship an looking at the asteroids)? Please help!
I understand how you have thing setup, but I don't understand what you are asking in this line:
Now, how i can position the aim plane in the way of real point to asteroids (directly forward the ship an looking at the asteroids)?
I'm asking that I need that the pointer must be aligned to invisible line that goes through the shooter and the target (the asteroids) so i can shoot to them and destroy them. If I make a plane child of the GameObject Ship, at position Z far from the ship$$anonymous$$esh, I only obtain a fixed aim that is in front of the ship$$anonymous$$esh but not in the traiectory of the target. I hope that you understand now...
You could try drawing the reticle on the GUI ins$$anonymous$$d of rendering a plane. These might help: http://docs.unity3d.com/Documentation/ScriptReference/Camera.WorldToScreenPoint.html http://docs.unity3d.com/Documentation/ScriptReference/GUI.DrawTexture.html
Ok, I've tried to do the aim in DrawTexture, but, what must be the target of Camera.WorldToScreenPoint() ? I've tried with "Vector3.Forward" and i obtain an aim in the center of the screen and not in front of the ship... and also its moving in y axis in inverted position...
Ok I inverted the y pos by subtracrting it by the Screen.height... but it conflicts with the SmoothFollow on the camera.. I used the target "new Vector3 (0, 0, 20)"...