I’ve noticed that the projectile is spawned at the centre of my Player’s Ship, but it would look better if it came from the nose of the spaceship, but i can’t seem to make it work. any advice?
public float speed = 10.0f;
public float padding = 0.1f;
public GameObject projectile;
public float projectileSpeed;
public float firingRate = 0.2f;
float xMin;
float xMax;
void Start (){
float distance = transform.position.z - Camera.main.transform.position.z;
Vector3 leftmost = Camera.main.ViewportToWorldPoint(new Vector3(0,0,distance));
Vector3 rightmost = Camera.main.ViewportToWorldPoint(new Vector3(1,0,distance));
xMin = leftmost.x + padding;
xMax = rightmost.x - padding;
}
void Fire (){
GameObject beam = Instantiate(projectile, transform.position, Quaternion.identity) as GameObject;
beam.GetComponent<Rigidbody2D>().velocity = new Vector3(0, projectileSpeed, 0);
}
void Update ()
{
if (Input.GetKeyDown (KeyCode.Space)) {
InvokeRepeating ("Fire", 0.00001f, firingRate);
}
if (Input.GetKeyUp (KeyCode.Space)) {
CancelInvoke ("Fire");
}
if (Input.GetKey (KeyCode.RightArrow)) {
transform.position += Vector3.right * speed * Time.deltaTime;
} else if (Input.GetKey (KeyCode.LeftArrow)) {
transform.position += Vector3.left * speed * Time.deltaTime;
}
//restricts player to gamespace
float newX = Mathf.Clamp(transform.position.x, xMin, xMax);
transform.position = new Vector3(newX, transform.position.y, transform.position.z);
}
}
I have tried to change the transfer.position
in the Instantiate(projectile, transform.position, Quaternion.identity) as GameObject;
to a different Vector3
coordinate, but it seems to break it. Anything else I could try?
Thanks in advance!