動く目標のバネ|Unity5

動く目標のバネ|Unity5

動く目標のバネ|Unity5 : ZIPファイル(522kb)

C#

Spring4.cs

using UnityEngine;
using System.Collections;

public class Spring4 : MonoBehaviour {

	float spring = 0.05f;
	float vx = 0;
	float vy = 0;
	float friction = 0.95f;

	// Use this for initialization
	void Start () {

	}

	// Update is called once per frame
	void Update () {
		Vector2 tapPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
		float mouseX = tapPoint.x;
		float mouseY = tapPoint.y;

		Vector2 v = transform.position;
		float dx = mouseX - v.x;
		float dy = mouseY - v.y;
		float ax = dx * spring;
		float ay = dy * spring;
		vx += ax;
		vy += ay;
		vx *= friction;
		vy *= friction;
		v.x += vx;
		v.y += vy;

		transform.position = v;
	}
}

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です