새총 발사까지 진행된 FarmerTouchControl.cs 파일 전체 코드
using UnityEngine;
using System.Collections;
public class FarmerTouchControl : MonoBehaviour {
public Camera mainCamera;
public GameObject fireObj;
public Transform firePoint;
Vector3 fireDirection;
public float fireSpeed = 3;
bool enableAttack = true;
Vector3 lastInputPosition;
Vector3 tempVector3;
Vector2 tempVector2 = new Vector2();
GameObject tempObj;
Animator animator;
void Awake()
{
animator = GetComponent<Animator>();
}
void Update()
{
if( Input.GetMouseButton(0) )
{
lastInputPosition = Input.mousePosition;
if( enableAttack )
{
animator.SetTrigger("fire");
}
}
}
void FireTrigger()
{
Fire(lastInputPosition);
}
void FireEnd()
{
enableAttack = true;
}
void Fire(Vector3 inputPosition)
{
tempVector3 = mainCamera.ScreenToWorldPoint(inputPosition);
tempVector3.z = 0;
fireDirection = tempVector3 - firePoint.position;
fireDirection = fireDirection.normalized;
tempObj = Instantiate(fireObj, firePoint.position,
Quaternion.LookRotation(fireDirection)) as GameObject;
tempVector2.Set(fireDirection.x, fireDirection.y);
tempVector2 = tempVector2 * fireSpeed;
tempObj.rigidbody2D.velocity = tempVector2;
}
}