The question is answered, right answer was accepted
2d cannon projectile not following expected path
Trying to: take rotation.z from parent, Cannon3. take this angle, convert to radians use this to create Vector2(Cos(rad), Sin(rad)) access slider value for power apply scalar power to vector2. use this vector2 in addForce.impulse to propel in arc... like a cannon
I swear Im just copy pasting at this point and not working right
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class CannonBallShooting : MonoBehaviour {
public GameObject cannonBall;
GameObject clone;
public Slider powerSlider;
public float force;
float radAngle;
Vector2 shootingVector;
// Use this for initialization
void Start () {
radAngle = Mathf.Deg2Rad * transform.rotation.z;
shootingVector = new Vector2(Mathf.Cos(radAngle), Mathf.Sin(radAngle));
}
// Update is called once per frame
void Update () {
Debug.Log(powerSlider.value);
}
public void Shoot()
{
clone = Instantiate(cannonBall, transform.position, transform.localRotation) as GameObject;
clone.transform.parent = this.transform;
clone.GetComponent<Rigidbody2D>().velocity =(shootingVector*powerSlider.value);
}
}
I' ve found my problem but cant figure out how to fix it.
I believe I am trying to apply a quarternion into a float, in the line angle = transform.rotation.z and then I am applying that into the vector as a float?
well, no. both transform.rotation.z and transform.rotation.eulerangles.z are floats.
so I dont understand why they dont work?
Im even just trying to use the following and its not printing the angle to the console. Ive also tried just accessing it with transform.rotation.z, then by getcomponent, then by a public transform. none of them went above 0 in the inspector.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class CannonBallShooting : $$anonymous$$onoBehaviour {
public GameObject cannonBall;
GameObject clone;
public Slider powerSlider;
public float force;
public Transform angleSource;
public float angle;
Rigidbody2D rg;
Vector2 shootingVector;
// Use this for initialization
void Start () {
rg = GetComponent<Rigidbody2D>();
angle = rg.transform.rotation.eulerAngles.y;
shootingVector = new Vector2($$anonymous$$athf.Cos(angle), $$anonymous$$athf.Sin(angle));
}
// Update is called once per frame
void Update () {
force = powerSlider.value;
Debug.Log(powerSlider.value);
Debug.Log(angle);
}
public void Shoot()
{
clone = Instantiate(cannonBall, transform.position, transform.localRotation) as GameObject;
clone.transform.parent = this.transform;
clone.GetComponent<Rigidbody2D>().AddForce(shootingVector*powerSlider.value, Force$$anonymous$$ode2D.Impulse);
Answer by sunderplugs11 · Mar 17, 2016 at 02:53 AM
I had it in Start, not in Update.
dont code while sleepy, kids.