When I remove the transform.Translate command, the arrow instantiates exactly where it’s supposed to. When I add the Translate command back in, the arrow appears so far out, it doesn’t even appear on the game map. I don’t understand why. Thanks for any pointers.
Here’s the script:
pragma strict
var arrowPrefab : Rigidbody;
var arrowSound : AudioClip;
var target : Transform;
var speed : float = 1.0;
var canShoot : boolean;
var origin : Transform;
var canPower : boolean;
var arrow : Rigidbody;
function Start ()
{
canPower = false;
}
function Update () {
}
function EnableShoot()
{
canShoot = true;
Shoot();
}
function Shoot ()
{
target = GameObject.FindWithTag("companionElf").transform;
arrow = Instantiate(arrowPrefab, origin.transform.position, origin.transform.rotation);
transform.LookAt(target.transform.position);
canPower = true;
canShoot = false;
}
function FixedUpdate()
{
if(canPower)
{
arrow.transform.Translate(Vector3 (0,0,speed));
Physics.IgnoreCollision(arrow.collider, transform.root.collider);
}
}
@script RequireComponent(AudioSource)
that’s probably because the model is rotated. In order to get this to work well, i would recommend:
function Awake () //called when the arrow is instantiated, this is different than Start()
{
arrow.rigidbody.AddForce(player.transform.forward*speed, ForceMode.VelocityChange);
}
(also, because you weren’t multiplying by Time.deltaTime before it may go really, really fast, so i recommend lowering the speed).
then you can apply gravity to the arrow so it lobs. The only other thing you would want to do is make the arrow’s rotation arcs along the direction of motion, which you should be able to do by Quaternion.SetLookRotation(arrow.rigidbody.velocity) in FixedUpdate or Update (and later as a coroutine when you get better, unless you already know about coroutines);
I’ve simply deleted the object and begun again. I have copied the script below on two separate objects in the game. On one object, the arrow instantiates as it is supposed to and flies properly; on the other, it does not. The scripts are identical. The problem must lie in the objects themselves. I am deleting the problematic object and starting from scratch.
#pragma strict
var canShoot : boolean;
var arrowPrefab : Rigidbody;
var speed : float = 500;
function Enable ()
{
yield WaitForSeconds(1.0);
canShoot = true;
}
function FixedUpdate ()
{
if(Input.GetButtonDown("Fire1") && canShoot == true)
{
var arrow2 : Rigidbody = Instantiate(arrowPrefab, transform.position, transform.rotation);
arrow2.velocity = transform.TransformDirection(Vector3 (0,0,speed));
Physics.IgnoreCollision(arrow2.collider, transform.root.collider);
canShoot = false;
}
}