Question by
Uair · Mar 03, 2016 at 04:59 PM ·
gameobjectinstantiatequaternionrigidbody2drigidbody.velocity
Trying to get objects to instantiate and move at different angles
I'm trying to make it so when you click left, three balls will fire from the sides, each having a slightly different angle to provide a bit of range. It's a 2D pirate ship game, hence the balls shoot from the sides instead of the front. But even though I was certain I got most of it right, they all merge into the same location. Here's the entire code, please take your time to look at it and maybe alter it a bit.
using UnityEngine;
using System.Collections;
public class player_canon_ball : MonoBehaviour {
public GameObject canonballPrefab;
public float fireDelay = 0.25f;
float cooldownTimerPort = 0f;
float cooldownTimerStarboard = 0f;
float fireSpeed = 5;
void Update()
{
cooldownTimerPort -= Time.deltaTime;
cooldownTimerStarboard -= Time.deltaTime;
if (Input.GetButton("Fire1") && cooldownTimerPort <= 0)
{
Debug.Log("Fire Port!");
cooldownTimerPort = fireDelay;
GameObject canonball1 = Instantiate(canonballPrefab, transform.position, Quaternion.Euler(0,-90,0)) as GameObject;
GameObject canonball2 = Instantiate(canonballPrefab, transform.position, Quaternion.Euler(0, -60, 0)) as GameObject;
GameObject canonball3 = Instantiate(canonballPrefab, transform.position, Quaternion.Euler(0, -120, 0)) as GameObject;
Rigidbody2D crb1 = canonball1.GetComponent<Rigidbody2D>();
Rigidbody2D crb2 = canonball2.GetComponent<Rigidbody2D>();
Rigidbody2D crb3 = canonball3.GetComponent<Rigidbody2D>();
crb1.velocity = canonball1.transform.forward * fireSpeed;
crb2.velocity = canonball2.transform.forward * fireSpeed;
crb3.velocity = canonball3.transform.forward * fireSpeed;
}
if (Input.GetButton("Fire2") && cooldownTimerStarboard <= 0)
{
Debug.Log("Fire Starboard!");
cooldownTimerStarboard = fireDelay;
Vector2 canon4 = new Vector2(transform.position.x, transform.position.y);
Vector2 canon5 = new Vector2(transform.position.x, transform.position.y);
Vector2 canon6 = new Vector2(transform.position.x, transform.position.y);
GameObject canonball4 = Instantiate(canonballPrefab, canon4, Quaternion.Euler(0, 90, 0)) as GameObject;
GameObject canonball5 = Instantiate(canonballPrefab, canon5, Quaternion.Euler(0, 120, 0)) as GameObject;
GameObject canonball6 = Instantiate(canonballPrefab, canon6, Quaternion.Euler(0, 60, 0)) as GameObject;
Rigidbody2D crb4 = canonball4.GetComponent<Rigidbody2D>();
Rigidbody2D crb5 = canonball5.GetComponent<Rigidbody2D>();
Rigidbody2D crb6 = canonball6.GetComponent<Rigidbody2D>();
crb4.velocity = canonball4.transform.forward * fireSpeed;
crb5.velocity = canonball5.transform.forward * fireSpeed;
crb6.velocity = canonball6.transform.forward * fireSpeed;
}
}
}
Take as much time as you need, I'm just making this for fun right now, no deadlines or anything.
Comment
Best Answer
Answer by Uair · Mar 03, 2016 at 05:11 PM
Fixed it! I had to fire right or left instead of forward. Just change "crb#.velocity = canonball#.transform.forward fireSpeed" to " "crb#.velocity = canonball#.transform.right fireSpeed"