Last active
November 10, 2020 08:50
-
-
Save Buravo46/7801252 to your computer and use it in GitHub Desktop.
【Unity】2D用の、ターゲットが居る方向に進んでいくスクリプト。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class TargetMoveScript : MonoBehaviour { | |
// 速度 | |
public Vector2 speed = new Vector2(0.05f, 0.05f); | |
// ターゲットとなるオブジェクト | |
public GameObject targetObject; | |
// ラジアン変数 | |
private float rad; | |
// 現在位置を代入する為の変数 | |
private Vector2 Position; | |
// Use this for initialization | |
void Start () { | |
// ラジアン | |
// atan2(目標方向のy座標 - 初期位置のy座標, 目標方向のx座標 - 初期位置のx座標) | |
// これでラジアンが出る。 | |
// このラジアンをCosやSinに使い、特定の方向へ進むことが出来る。 | |
rad = Mathf.Atan2( | |
targetObject.transform.position.y-transform.position.y, | |
targetObject.transform.position.x-transform.position.x); | |
} | |
// Update is called once per frame | |
void Update () { | |
// 現在位置をPositionに代入 | |
Position = transform.position; | |
// x += SPEED * cos(ラジアン) | |
// y += SPEED * sin(ラジアン) | |
// これで特定の方向へ向かって進んでいく。 | |
Position.x += speed.x * Mathf.Cos(rad); | |
Position.y += speed.y * Mathf.Sin(rad); | |
// 現在の位置に加算減算を行ったPositionを代入する | |
transform.position = Position; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma strict | |
// 速度 | |
var speed:Vector2 = Vector2(0.05, 0.05); | |
// ターゲットとなるオブジェクト | |
var targetObject:GameObject; | |
// プライベート変数なラジアン変数 | |
private var rad:float; | |
function Start () { | |
// ラジアン | |
// atan2(目標方向のy座標 - 初期位置のy座標, 目標方向のx座標 - 初期位置のx座標) | |
// これでラジアンが出る。 | |
// このラジアンをCosやSinに使い、特定の方向へ進むことが出来る。 | |
rad = Mathf.Atan2( | |
targetObject.transform.position.y-transform.position.y, | |
targetObject.transform.position.x-transform.position.x); | |
} | |
function Update () { | |
// x += SPEED * cos(ラジアン) | |
// y += SPEED * sin(ラジアン) | |
// これで特定の方向へ向かって進んでいく。 | |
transform.position.x += speed.x * Mathf.Cos(rad); | |
transform.position.y += speed.y * Mathf.Sin(rad); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment