Hey again. I’m using this:
var missle : Transform;
function Update () {
var accel = Input.GetAxis("Vertical")*3;
var steering = Input.GetAxis("Horizontal")/2;
if (Input.GetButtonDown ("Fire")) {
Instantiate (missle, Vector3(transform.position.x, transform.position.y+.93, transform.position.z+2), transform.rotation);
}
rigidbody.AddRelativeForce(0, 0, accel);
transform.Rotate(0, steering, 0);
}
To instantiate missiles when you press the correct button. But the problem is that when you’re moving, or especially the missiles don’t come out of their appropriate place. What should I do to get them to always appear in the same relative spot.
You probably want to instantiate your missiles in front of the car, or on top of it. To do this, you would use Instantiate(missle, transform.position + transform.forward, transform.rotation);
or Instantiate(missle, transform.position + transform.up, transform.rotation);
This will place the missile either one unit in front of you, or one unit above. If you want the missile closer or further away, multiply the offset vector (for instance, transform.up) by the distance you want.
I got something that nearly works!
var missle : Transform;
function Update () {
var accel = Input.GetAxis("Vertical")*3;
var steering = Input.GetAxis("Horizontal")/2;
if (Input.GetButtonDown ("Fire")) {
Instantiate(missle, Vector3(transform.position.x, transform.position.y+0.8, transform.position.z+1), transform.rotation);
}
rigidbody.AddRelativeForce(0, 0, accel);
transform.Rotate(0, steering, 0);
var tagToIgnore = "missile";
var layerColliders = Array();
layerColliders = GameObject.FindGameObjectsWithTag (tagToIgnore);
for (var layerCollider in layerColliders)
{
if(layerCollider.collider gameObject.collider != layerCollider.collider) Physics.IgnoreCollision(collider, layerCollider.collider);
}
}
Now they don’t send the tank flying all over the place, but they still don’t come out of the correct spot when rotating…
Try this:
if (Input.GetButtonDown ("Fire")) {
var pos = transform.TransformPoint (Vector3 (0, 0.93, 2));
Instantiate (missle, pos, transform.rotation);
}
Yahoo! You’re a life saver!
Thanks again!