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 /
avatar image
0
Question by kianoush1998 · Jul 27, 2018 at 07:29 AM · 2d gameshooting2d-physics

shooting object from from multiple locations

hello everyone ,, I am new to unity 2d and I have been following the tutorial about angry bird style game and I wanted to edit some gameplay of the game for my own project here is my problem when we shoot the object from the catapult I want to catch the object from a different location and then shoot the object again from that location I am working on a top-down 2d game that is like that shooting the object and catches it and then shoots again. here is my code most of it is like the tutorial and I kind of ruined the editing

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class firing : MonoBehaviour {

 public float MaxStretch = 3f;
 public LineRenderer CatapultFront;
 public LineRenderer CatapultBack;
 public Transform catapult;

 private bool ClickedOn;
 private Rigidbody2D Rigid;
 private SpringJoint2D spring;
 private Ray RayToMouse;
 private Ray LeftCatapultToProjectile;
 private Vector2 PreVelocity;
 private float CircleRadius;
 private float maxS;
 private bool entering;
 
 


 private void Awake()
 {
     
     Rigid = GetComponent<Rigidbody2D>();
     spring = GetComponent<SpringJoint2D>();



 }


 void Start () {
     
     catapult = spring.connectedBody.transform;
     LineRendereSetup();
     RayToMouse = new Ray(catapult.transform.position, Vector3.zero);
     LeftCatapultToProjectile = new Ray(CatapultFront.transform.position, Vector3.zero);
         maxS = MaxStretch * MaxStretch;
     // PreVelocity = Rigid.velocity;
     CircleCollider2D circle = GetComponent<Collider2D>() as CircleCollider2D;
     CircleRadius = circle.radius;
   Rigid.gravityScale = 0.0f;

 }

 // Update is called once per frame
 void Update()


 {
     LineRendererUpdate();
     if (ClickedOn)
         Dragging();
         if(spring.enabled==true)
         {
        
         if (!Rigid.isKinematic  &&  PreVelocity.sqrMagnitude > Rigid.velocity.sqrMagnitude) {
             // Debug.Log(Rigid.isKinematic);
             spring.enabled = false;
                 Rigid.velocity = PreVelocity;

             }
         if (!ClickedOn)
         {
           PreVelocity = Rigid.velocity;
             //Debug.Log(PreVelocity);

             CatapultFront.enabled = false;
             CatapultBack.enabled = false;

         }




     }
         if(spring.enabled ==false && entering)
     {
         



     }
         

     
     
     


 }
 
 private void OnMouseDown()
 {
     
     
         spring.enabled = false;
         ClickedOn = true;
     }
 
 private void OnMouseUp()
 {
     
     
         //  Debug.Log("!clicked");
         spring.enabled = true;
         Rigid.isKinematic = false;
         ClickedOn = false;
     
 }



 protected void LineRendereSetup()
 {
     CatapultFront.SetPosition(0, CatapultFront.transform.position);
     CatapultBack.SetPosition(0, CatapultBack.transform.position);

     CatapultFront.sortingLayerName = "foreground";
     CatapultBack.sortingLayerName = "foreground";

     CatapultFront.sortingOrder = 3;
     CatapultBack.sortingOrder = 1;
    // Debug.Log("band");
 }
 void Dragging()
 {
  //   Touch touch = Input.GetTouch(0);
     Vector3 mouseworldpoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
     Vector2 CatapultToMouse = mouseworldpoint - catapult.position;
     if(CatapultToMouse.sqrMagnitude > maxS)
     {
         RayToMouse.direction = CatapultToMouse;
         mouseworldpoint = RayToMouse.GetPoint(MaxStretch);
        // Debug.Log("Salam");
     }
     mouseworldpoint.z = 0f;
     transform.position = mouseworldpoint;
 }

private void LineRendererUpdate() { // Debug.Log("render"); Vector2 CatapultToProjectile = transform.position - CatapultFront.transform.position; LeftCatapultToProjectile.direction = CatapultToProjectile; Vector2 HoldPoint = LeftCatapultToProjectile.GetPoint(CatapultToProjectile.magnitude +CircleRadius); CatapultFront.SetPosition(1,HoldPoint); CatapultBack.SetPosition(1, HoldPoint); } private void OnTriggerEnter2D(Collider2D collision) { catapult.position = collision.transform.position; spring.connectedBody = null; spring.connectedBody = collision.GetComponent(); // ClickedOn = true; OnMouseDown(); Debug.Log("salam");

     OnMouseUp();

 }

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by madks13 · Jul 27, 2018 at 08:23 AM

Without trying to read the mess of code you posted, i'd say you need to make an object that has a radial collider. When your projectile enters the collider, bring it to the ejection point (center of object, launch pad, whatever...), then launch again in wanted direction.

That requires a new object and a new script.

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

102 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 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 make my space ship shooting (2D Game)? 0 Answers

Two Polygon2D Collider do not Collide with Each Other (Solved) 2 Answers

Play and Stop Animation 1 Answer

I am very new to unity and I'm looking for advice on a targeting system( or if i'm setting myself up for failure) 1 Answer

My 2D character is falling through the ground 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