how do i launch my player out of the cannon
so i have a cannon with OntriggerStay2D attached and when my player jump into it, it's parented to that cannon, Then i can launch that player out when fire1 is pressed which that works fine. The problem i am having is that my player still get launched when he is not even inside the cannon when fire1 button is pressed. For example, the player is on the ground away from the cannon and when i pressed the fire1 button, the player get transported into the cannon and being fired afterward which is weird.
So how do i make sure the player can only get launched after he is inside the cannon? here's my script on the cannon
void Update ()
{
tempPos.y = startPosition + amplitude * Mathf.Sin (speed * Time.time);
transform.position = tempPos;
transform.Rotate (0, 0, 300 * Time.deltaTime);
if (Input.GetButtonDown ("fire1") && !isShooting)
{
gameObject.GetComponent<Collider2D> ().enabled = false;
transform.DetachChildren();
character.transform.position = transform.position;
character.transform.rotation = aim.rotation;
rb2D.AddForce (Vector2.up, ForceMode2D * 2000);
character.gameObject.GetComponent<Rigidbody2D> ().gravityScale = 1f;
isShooting = true;
}
}
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<TurtleController> ().enabled = false;
other.gameObject.GetComponent<Rigidbody2D> ().gravityScale = 0f;
}
}
Answer by Ali-hatem · Apr 15, 2016 at 10:27 AM
bool isShooting = false;
void Update ()
{
if (Input.GetButtonDown ("fire1") && isShooting)
{
//do your stuffs
isShooting = false;
}
}
void OnTriggerStay2D(Collider2D other)
{
if (other.gameObject.CompareTag ("Player"))
{
//do your stuffs
isShooting = true;
}
}
Answer by FlavioVolpeJr · Oct 16, 2020 at 08:17 PM
I spent about 3 days trying to make this script, now that I got it, it has worked well in my project. Here he goes to help anyone looking for the same thing. Remembering that it is for 3D games.
using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PlayerControlCannon : MonoBehaviour
{
public Rigidbody character;
public Transform shootingPart;
public bool manualControl;
public float rotatingSpeed = 100f;
public float shotForce = 50f;
private bool isShooting = false;
private float backColliderDelay = 0.05f;
public void Start()
{
}
public void Update()
{
if (Input.GetButtonDown("Fire1") && isShooting == true)
{
StartCoroutine("Shooting");
print("Fired");
}
}
IEnumerator Shooting()
{
character.transform.position = transform.position;
character.transform.rotation = shootingPart.rotation;
character.AddForce(transform.forward * shotForce, ForceMode.VelocityChange);
character.transform.parent = GameObject.FindWithTag("Player").transform;
character.GetComponent<MeshRenderer>().enabled = true;
character.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;
character.GetComponent<Collider>().enabled = false;
yield return new WaitForSeconds(backColliderDelay);
character.GetComponent<Collider>().enabled = true;
isShooting = false;
}
public void OnTriggerStay(Collider 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<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;
other.gameObject.GetComponent<MeshRenderer>().enabled = false;
isShooting = true;
}
if (manualControl == true)
{
// Move to the right
if (Input.GetButton("Horizontal") && Input.GetAxisRaw("Horizontal") > 0)
{
transform.Rotate(Vector3.up * rotatingSpeed * Time.deltaTime);
}
// Move to the left
else if (Input.GetButton("Horizontal") && Input.GetAxisRaw("Horizontal") < 0)
{
transform.Rotate(Vector3.down * rotatingSpeed * Time.deltaTime);
}
// Move to the up
else if (Input.GetButton("Vertical") && Input.GetAxisRaw("Vertical") < 0)
{
transform.Rotate(Vector3.right * rotatingSpeed * Time.deltaTime);
}
// Move to the down
else if (Input.GetButton("Vertical") && Input.GetAxisRaw("Vertical") > 0)
{
transform.Rotate(Vector3.left * rotatingSpeed * Time.deltaTime);
}
}
}
}