The question is answered
how to fire my player out of a Cannon?
I finally got the character control working. I made it jump into a cannon lock and loaded which is also working perfectly thanks to a lot of people who guided me in here. I'm very new with scripting but I actually understand a lot thanks to the tutorial and especially in this community. Much appreciated really >< I have this scrip attached to my Cannon
public class Barrel : MonoBehaviour
{
public float amplitude;
public float speed;
private float startPosition;
private Vector3 tempPos;
public Transform aim;
public float thrust;
void Start ()
{
startPosition = transform.position.y;
tempPos = transform.position;
}
void Update ()
{
tempPos.y = startPosition + amplitude * Mathf.Sin (speed * Time.time);
transform.position = tempPos;
transform.Rotate (0, 0, 300 * Time.deltaTime);
}
void OnTriggerStay2D(Collider2D other)
{
if (other.gameObject.CompareTag ("Player"))
{
other.gameObject.transform.parent = transform;
other.gameObject.transform.localPosition = new Vector3 (0f, 0f, 0f);
other.gameObject.transform.localRotation = Quaternion.identity;
other.gameObject.GetComponent<Rigidbody2D> ().gravityScale = 0f;
other.gameObject.GetComponent<TurtleController> ().enabled = false;
if (Input.GetKeyDown (KeyCode.Space))
other.gameObject.GetComponent<Rigidbody2D> ().AddForce(-aim.forward * thrust);
}
}
}
and I am having difficulties with launching my player out of the Cannon. I tried testing this to launch my player. ( this script is attached to cannon)
if (Input.GetKeyDown (KeyCode.Space))
Instantiate(player, aim.position, aim.rotation);
other.gameObject.GetComponent<Rigidbody2D> ().AddForce(-aim.forward * thrust);
}
}
}
but it's duplicating my player every time i pressed the Space button which i don't want. I have a Camera attached to the player as a child and when it's duplicate it, it duplicate the camera too and it's messed up. Reason i am using Instantiate is because the Cannon is Constantly moving back and forth in 2D and it's also rotating. There is a arrow texture on the Cannon pointing only one direction and whenever the Fire Button is pressed, the player will be launch at where ever the arrow is pointing. So i thought I needed Instantiate and create an empty child Gameobject to the cannon to help me create a aim position on the cannon. Not sure if i even need the Instantiate, i only know how to use it through the tutorial in spaceshooter plus some help in the community.
Also another problem is when i tried launching the player out, my player is frozed inside the Cannon, and I think the problem might be the OnTriggerStay2D, I have a circle collider2D attached to the Cannon and it's probably locking the player inside the collider and i'm not sure how to turn it off when launching my player out. I needed the OnTriggerStay2D to stay active until when the player is needed to be launched out of the cannon though. Anyone know a easy way to fix this ><;
Follow this Question
Related Questions
Using rb.velocity and Add Force on the same gameobject? 0 Answers
Rigidbody Addforce not working as expected ( video included) 0 Answers
Are AddForce calculate different depend on CPU performance ? 0 Answers
How to stop the character moving after the button is pressed, using Rigidbody2D()..AddForce? 1 Answer
AddForce rigidbody 2d not moving object 2 Answers