《愤怒的小鸟》弹弓实现

using UnityEngine;
using System.Collections;

public class Ball : MonoBehaviour {
    private Vector3 MousePosition;

    private LineRenderer leftLine;
    private LineRenderer rightLine;

    void Start()
    {
        leftLine = GameObject.Find("Shootor").transform.Find("RopeL").transform.GetComponent<LineRenderer>();
        leftLine = GameObject.Find("Shootor").transform.Find("RopeR").transform.GetComponent<LineRenderer>();
    }

    void Update()
    {
        if(Input .GetMouseButton (0))
        {
            MousePosition = Camera.main.ScreenToViewportPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, -2f));
            transform.position = MousePosition;

            leftLine.SetPosition(0, new Vector3(MousePosition.x, MousePosition.y, MousePosition.z - 0.5f));
            rightLine.SetPosition(0, new Vector3(MousePosition.x, MousePosition.y, MousePosition.z - 0.5f));
        }
        if(Input .GetMouseButtonUp (0))
        {
            MousePosition = Camera.main.ScreenToViewportPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, -2f));
            transform.position = MousePosition;

            leftLine.SetPosition(0, new Vector3(MousePosition.x, MousePosition.y, MousePosition.z - 0.5f));
            rightLine.SetPosition(0, new Vector3(MousePosition.x, MousePosition.y, MousePosition.z - 0.5f));


            Vector3 left = new Vector3(-2f - MousePosition.x, 1.8f - MousePosition.y, -MousePosition.z);
            Vector3 right = new Vector3(2f - MousePosition.x, 1.8f - MousePosition.y, -MousePosition.z);

            Vector3 dir = (left + right).normalized;

            transform.GetComponent<Rigidbody>().useGravity = true;
            transform.GetComponent<Rigidbody>().AddForce(dir * 10f, ForceMode.Impulse);

            leftLine.SetPosition(0, new Vector3(0, 1.8f, 0));
            rightLine.SetPosition(0, new Vector3(0, 1.8f, 0));
        }
    }
	
}

原文链接 : 点击打开链接


阅读更多

没有更多推荐了,返回首页