I’ve been learning unity and following along with the tutorial here
I’ve completed part one of the tutorial and can’t seem to figure out why my projectiles don’t shoot. I’ve compared my script to the one found in the example game of the asset store, and I don’t see a difference (except for the fact I used sling instead of catapult – this is reflected in my scene too).
Here’s a gif of what’s happening – it’s like the projectile doesn’t move forward at all… http://g.recordit.co/sd03w47xjd.gif
Here’s my current script – did I miss something in the unity scene editor maybe? Many thanks in advance for any help / suggestions!
public class ProjectileDragging : MonoBehaviour {
public float maxStretch = 3.0f;
public LineRenderer slingLineFront;
public LineRenderer slingLineBack;
private SpringJoint2D spring;
private Ray rayToMouse;
private Ray leftSlingToProjectile;
private float maxStretchSqr; // max stretch to power of 2
private Rigidbody2D rb;
private CircleCollider2D circle;
private float circleRadius;
private Transform sling;
private bool clickedOn;
private Vector2 prevVelocity;
void Awake()
{
rb = GetComponent<Rigidbody2D>();
spring = GetComponent<SpringJoint2D>();
circle = GetComponent<CircleCollider2D>();
sling = spring.connectedBody.transform;
}
// Use this for initialization
void Start()
{
LineRendererSetup();
rayToMouse = new Ray(sling.position, Vector3.zero);
leftSlingToProjectile = new Ray(slingLineFront.transform.position, Vector3.zero);
maxStretchSqr = maxStretch * maxStretch;
circleRadius = circle.radius;
}
// Update is called once per frame
void Update()
{
// when clicking on the projectile, enable the drag behaviors
if (clickedOn) {
Dragging();
}
// if we have a spring attached
if (spring != null) {
// if we've released the projectile, but its velocity is starting to slow down
if (! rb.isKinematic && prevVelocity.sqrMagnitude > rb.velocity.sqrMagnitude) {
Destroy(spring);
rb.velocity = prevVelocity;
}
// Set our previous velocity variable for the next frame;
if (!clickedOn) {
prevVelocity = rb.velocity;
}
LineRendererUpdate();
} else {
slingLineFront.enabled = false;
slingLineBack.enabled = false;
}
}
/**
* Setup the line renderers for the sling's rubber bands
*/
void LineRendererSetup()
{
// Set starting position
slingLineFront.SetPosition(0, slingLineFront.transform.position);
slingLineBack.SetPosition(0, slingLineBack.transform.position);
// Set sorting layer as 'foreground'
slingLineFront.sortingLayerName = "Foreground";
slingLineBack.sortingLayerName = "Foreground";
// Set the order within the layer
slingLineFront.sortingOrder = 3;
slingLineBack.sortingOrder = 1;
}
void OnMouseDown()
{
spring.enabled = false;
clickedOn = true;
}
void OnMouseUp()
{
spring.enabled = true;
rb.isKinematic = false;
clickedOn = false;
}
// Dragging the asteroid
void Dragging()
{
// get the point in game space based on mouse position
Vector3 mouseWP = Camera.main.ScreenToWorldPoint(Input.mousePosition);
// get sling position compared to the mouse
Vector2 slingToMouse = mouseWP - sling.position;
// check if we're max distance on the pull, if it is
// then we set the ray direction to be toward the mouse and
// the asteroid on a point at the max stretch value within that ray
if (slingToMouse.sqrMagnitude > maxStretchSqr) {
rayToMouse.direction = slingToMouse;
mouseWP = rayToMouse.GetPoint(maxStretch);
}
// set z to zero, this is 2D
mouseWP.z = 0f;
// set the transform of this object to be the mouse's position
transform.position = mouseWP;
}
void LineRendererUpdate()
{
Vector2 slingToProjectile = transform.position - slingLineFront.transform.position;
leftSlingToProjectile.direction = slingToProjectile;
Vector3 holdPoint = leftSlingToProjectile.GetPoint(slingToProjectile.magnitude + circleRadius);
slingLineFront.SetPosition(1, holdPoint);
slingLineBack.SetPosition(1, holdPoint);
}
}