Witam,tworzę sobie grę w stylu Angry Birds z pomocą tego tutoriala http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/making-angry-birds-style-game
Jestem na 55 minucie,ale gdy wciskam przycisk Play to gdy najeżdżam na procę i próbuję na nią kliknąć i przytrzymać by przygotować się do wystrzału to z procą zwyczajnie nic się nie dzieje.
Poniżej zamieszczam kod skryptu odpowiadającego za wystrzał.Jeśli coś jeszcze będzie potrzebne to napiszcie,od razu to wkleje.
using UnityEngine;
using System.Collections;
public class ProjecttileDragging : MonoBehaviour {
public float maxStretch = 3.0f;
public LineRenderer catapultLineFront;
public LineRenderer catapultLineBack;
private Transform catapult;
private SpringJoint2D spring;
private bool clickedOn;
private Ray rayToMouse;
private Ray LeftCatapultToProjectTile;
private float maxStretchSqr;
private float circleRadius;
private Vector2 prevVelocity;
// Use this for initialization
void Awake()
{
spring = GetComponent<SpringJoint2D> ();
catapult = spring.connectedBody.transform;
}
void Start () {
LineRendererSetup ();
rayToMouse = new Ray (catapult.position, Vector3.zero);
LeftCatapultToProjectTile = new Ray (catapultLineFront.transform.position,Vector3.zero);
maxStretchSqr = maxStretch * maxStretch;
CircleCollider2D circle = collider2D as CircleCollider2D;
circleRadius = circle.radius;
}
// Update is called once per frame
void Update () {
if (clickedOn)
Dragging ();
if (spring != null)
{
if (!rigidbody2D.isKinematic && prevVelocity.sqrMagnitude > rigidbody2D.velocity.sqrMagnitude)
{
Destroy(spring);
rigidbody2D.velocity = prevVelocity;
}
if (!clickedOn)
{
prevVelocity = rigidbody2D.velocity;
}
LineRendererUpdate();
}
else
{
catapultLineFront.enabled = false;
catapultLineBack.enabled = false;
}
}
void onMouseDown()
{
spring.enabled = false;
clickedOn = true;
}
void onMouseUp()
{
spring.enabled = true;
rigidbody2D.isKinematic = false;
clickedOn = false;
}
void Dragging()
{
Vector3 mouseWorldPoint = Camera.main.ScreenToWorldPoint (Input.mousePosition);
Vector2 catapultToMouse = mouseWorldPoint - catapult.position;
if (catapultToMouse.sqrMagnitude > maxStretchSqr)
{
rayToMouse.direction = catapultToMouse;
mouseWorldPoint = rayToMouse.GetPoint(maxStretch);
}
mouseWorldPoint.z = 0f;
transform.position = mouseWorldPoint;
}
void LineRendererUpdate()
{
Vector2 CatapultToProjectile = transform.position - catapultLineFront.transform.position;
LeftCatapultToProjectTile.direction = CatapultToProjectile;
Vector3 holdPoint = LeftCatapultToProjectTile.GetPoint (CatapultToProjectile.magnitude + circleRadius);
catapultLineFront.SetPosition (1, holdPoint);
catapultLineBack.SetPosition (1, holdPoint);
}
void LineRendererSetup()
{
catapultLineFront.SetPosition (0, catapultLineFront.transform.position);
catapultLineBack.SetPosition (0, catapultLineBack.transform.position);
catapultLineFront.sortingLayerName = "ForeGround";
catapultLineBack.sortingLayerName = "ForeGround";
catapultLineBack.sortingOrder = 3;
catapultLineFront.sortingOrder = 1;
}
}