今回は,敵の作成に行きます!!
以下,完成したもの!!
こちらを見てください
こちらは,私が今現段階でunity roomのバーションのゲームの敵の動きです.
端に行くと一応むきが反転するようにコードの中で計算をしてやっていますが,動きがダサいですよね?
(一応,このスクリプトをあげておきます)
void move_side(){
// 縦の動きをする猫の動き
float pos_y = this.gameObject.transform.position.y;
float pos_x = this.gameObject.transform.position.x;
// Debug.Log(pos_x);
float sin = Mathf.Sin(rotation_speed*Time.time)*-1*rotation_range + position_x;
Vector2 temp = transform.localScale;
if(sin - position_x > 0.99){
temp.x = 2;
}else if(sin - position_x < -0.99){
temp.x = -2;
}
transform.localScale = temp;
// Debug.Log("opossum->"+"pos_x:"+this.gameObject.transform.localPosition.x+"
,pos_y"+this.gameObject.transform.localPosition.y+",sin:"+sin);
this.transform.position = new Vector3(sin,pos_y,0);
}
こちらが,上の猫の動きになります!!
一応sinで最大値のところになる頃に反転をしているのですがうまくは行きませんでした.
そこで,DOTween のDOLocalPathを使っていきます
まず,何をしたいのかを簡単に述べておきます.
・猫を左右の動きにしたい
・端に行ったら向きを端の瞬間に変えて欲しい
・色々と動きを制御したい
では,どのようにしたのか!!
こちらがコードになります.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using DG.Tweening;
public class enemy : MonoBehaviour
{
private Sequence sequence;
private Transform trans;
void Start(){
// Debug.Log("hello");
trans = GetComponent<Transform>();
sequence = DOTween.Sequence();
if(gameObject.layer == 9) a();
}
// Update is called once per frame
void FixedUpdate(){
// Debug.Log("hello2");
}
void MoveSide(){
sequence.InsertCallback(
0.0f,
a
);
// sequence.InsertCallback(
// 3.0f,
// b
// );
}
void a(){
Vector3[] paths = {
new Vector3(-9.1f,0.295f),
new Vector3(-13.4f,0.295f),
new Vector3(-9.1f,0.295f),
};
trans.DOLocalPath(paths,3.0f,PathType.CatmullRom)
.SetLookAt(0.03f, new Vector3(9.1f,0.0f,0.0f))
.SetLoops(-1)
.SetEase (Ease.Linear);
}
}
関数を分けているのは,見やすいためなので気にしないでください!!
説明とは,下の参考にしたページに書いてあるのでそちらをご覧ください
ほとんどのサイトでは3Dでの説明になっていたので,今回苦労したことを書いていきます.
まず,SetLookAtでそのキャラクターが同じところを見続けることができます.
その中で場所を指定してあげることができるので,関数の中で位置を指定します
new Vector3(9.1f,0.0f,0.0f))
これは,移動するときの真ん中の座標でした(クリックをして調べました)
(変数にすればもっと楽かも??)
以上で,上のようなものになります.
今回のものはDOTween Proじゃなくてもできるはずです....
参考にしたサイト