Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
This question was closed Apr 15, 2016 at 05:50 AM by Dqimass for the following reason:

The question is answered

avatar image
0
Question by Dqimass · Apr 11, 2016 at 02:09 PM · playerrigidbody2dcollider2daddforceontriggerstay

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 ><;

Comment
Add comment · Show 1
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 05:50 AM 0
Share

actually nvm, i figured it out :P

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

59 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 avatar image avatar image

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


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