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();
}
}