//tips
//animationでimageを特定位置に動かす
animationのAnchored Positionから座標を変更する。Rotationで動きを加える。
各ボタン位置を静止モーションとして間に挟み、ボタン間移動をtriggerで管理した。
Animationを起動しっぱなしにして動きに対応している。
今回はspaceキーとaキーでtriggerとなるanimator.SetTriggerを呼び出している。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AnimationController : MonoBehaviour
{
Animator animator;
void Start()
{
this.animator = GetComponent<Animator>();
}
void Update()
{
if (Input.GetKeyDown("space"))
{
animator.SetTrigger("Go");
}
if (Input.GetKeyDown("a"))
{
animator.SetTrigger("Back");
}
}
}
今度はこれをキー操作ではなく、マウスポイントの位置がボタンの上に来た場合にanimationを起こすようにする。
ボタンに対して、animation controllerを追加して移動を制御する。
ボタン2の上にポイントがきたら animator.SetTrigger("Go”);が発動し、ボタン1の上にポイントがきたらanimator.SetTrigger("Back”);が発動するようにする。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class AnimationController2 : MonoBehaviour
{
public GameObject obj;
Animator anim;
void Start()
{
obj = GameObject.Find("Imagefollow");
anim = obj.GetComponent<Animator>();
}
void OnMouseEnter()
{
anim.SetTrigger("Go");
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class AnimationController1 : MonoBehaviour
{
public GameObject obj;
Animator anim;
void Start()
{
obj = GameObject.Find("Imagefollow");
anim = obj.GetComponent<Animator>();
}
void OnMouseEnter()
{
anim.SetTrigger("Back");
}
}
ただ、このtriggerによるanimation移動はボタンを飛ばして選択した時やenter範囲から出て再度同じボタンに戻ってきたときにもアクションが行われることから、このやり方を押し通す場合、より多岐にわたる設定が必要となることがわかる。
一旦考え方をマウスポイントへの座標移動に戻し、別の方法を模索する。
アクションをつけたい場合は、後ほど移動と関係なく時間経過で付加すれば良い。
//Vector3.Lerpで起点と終点の間を補完する
アクションをVector3.Lerpを使用したtransform.positionで行えないか考える。
まずは緑cubeを緑cubeの位置まで動かす。
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Liner : MonoBehaviour
{
//スタートと終わりの目印
public Transform startMarker;
public Transform endMarker;
// スピード
public float speed = 1.0F;
//二点間の距離を入れる
private float distance_two;
void Start()
{
//二点間の距離を代入(スピード調整に使う)
distance_two = Vector3.Distance(startMarker.position, endMarker.position);
}
void Update()
{
// 現在の位置
float present_Location = (Time.time * speed) / distance_two;
// オブジェクトの移動
transform.position = Vector3.Lerp(startMarker.position, endMarker.position, present_Location);
}
}
同様の動きをUIのimageで行ったのだが初期地点から動かないバグが出てきてしまったので現在確認中。