I’m trying to allow a floating cannon be player controlled and move 360 degrees. The projectile shoots out depending on the angle. So, I tried making a vector of x component being Mathf.Cos(this.transform.rotation.x * Mathf.DegToRad) * speed and y being the same with Sin, but that wouldn’t work. The force is also an impulse. I mean, it would work but it would only be a positive shot and even at 90 degrees it wouldn’t shoot straight up.
I looked up some stuff and saw people say to use transform.forward, but that isn’t working either. This is a 2D game by the way in case it wasn’t obvious.
using UnityEngine;
using System.Collections;
public class CannonController : MonoBehaviour {
public Rigidbody2D cannonRB;
//public GameObject cannon;
public Transform cannonRotation;
public bool controlled;
public GameObject shotLocation;
public Vector2 shootLocation;
public GameObject projectile;
public Rigidbody2D rb;
public Collider2D projcollider;
public float speed;
private int timeoutDestructor;
// Use this for initialization
void Start () {
cannonRotation = GetComponent<Transform> ();
cannonRB = GetComponent<Rigidbody2D> ();
//cannon = GetComponent<GameObject> ();
controlled = true;
}
// Update is called once per frame
void Update () {
shootLocation = new Vector2(shotLocation.transform.position.x, shotLocation.transform.position.y);
if (controlled == true) {
if (Input.GetKey (KeyCode.A)) {
transform.Rotate (Vector3.forward * 100 * Time.deltaTime);
}
if (Input.GetKey (KeyCode.D)) {
transform.Rotate (Vector3.back * 100 * Time.deltaTime);
}
if (Input.GetKeyDown (KeyCode.Space)) {
//timeoutDestructor = 1;
Launch ();
//controlled = false;
}
}
}
void Launch(){
//cannon = GetComponent<GameObject> ();
GameObject clone = Instantiate (projectile, shootLocation, Quaternion.identity) as GameObject;
Rigidbody2D clonerb = clone.GetComponent<Rigidbody2D> ();
clonerb.AddForce (transform.forward * speed, ForceMode2D.Impulse);
}
}
2D is only on the X/Y planes, not Z. So using transform.forward (Z) won’t work. You can use AddRelativeForce if you want to use the object’s coordinates.
I would think if you can make the cannon rotate 360 degrees, just make the projectile a kid of the cannon. So when cannon rotates it rotates with it. and also you could do
make a 2D invisible sprite in the cannon tip.
make the projectile follow the tip where it fires or whatever you make it do.
rotate the projectile much as the cannon was rotated
if cannon rotated 39.9 make projectile rotate 39.9 while projectile go to cannon tip.
Isn’t giving me anything but the same force direction? Shouldn’t transform.rotation.z (script is attached to rotating cannon gameobject) be changed as the object is rotated? Should I make a new variable in update of that transform.rotation.z?
The projectile is firing from the end of the cannon but in the same direction with the same force ever time I hit space.
I have an empty game object on the tip of the cannon that I use as the position to shoot the instantiated object from. Why does it matter if I have to rotate my sprite when I’m just trying to shoot it at the same angle as the cannon (which is why im getting my z angle from the cannon)? My instantiated object is just an oval at the moment so I’m not too worried about the rotation of the look of the object.