• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Dqimass · Apr 15, 2016 at 06:49 AM · transform.positionvector2ontriggerstaygetbuttondownforcemode

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;
 

     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
      }
  }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dqimass · Apr 15, 2016 at 08:30 PM 0
Share

thank you!

avatar image
0

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);
              }     
          }
      }
  }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

57 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to add a particular distance to transform.position 1 Answer

How to move more than one object together at a time? 1 Answer

Velocity X and Y speed limits 1 Answer

JohnyControllerScript.cs(20,17): error CS0029: Cannot implicitly convert type `UnityEngine.Vector2' to `UnityEngine.Rigidbody2D' 0 Answers

How to use Vector2.Lerp in two arrays 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges