code ソーシャル

Unityアプリcodetips(36)

スポンサーリンク

//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で行ったのだが初期地点から動かないバグが出てきてしまったので現在確認中。

人気の記事

1

皆さん、ついに、エアラインでも、サブスクリプションが始まったのはご存じですか? まだ実験段階ですが、ANAが、定額全国住み放題サービスを提供する「ADDress」と組んで、国内線を4回まで定額利用可能 ...

2

無料でネットショップを開けるアプリとして多くの人に驚きを与えたBASE株式会社が、2019年10月25日東証マザーズに上場しました。2020年2月時点で90万店を超えるショップを抱えるまでに成長してい ...

3

2011年にサービスを開始してから圧倒的な成長率を誇るインテリア通販サイト 【FLYMEe/フライミー】を皆さんご存じでしょうか。 「自分のイメージするインテリア、本当に欲しいインテリアがどこにあるの ...

4

ついに、noteの月間アクティブユーザー数が4400万人(2020年3月時点)に到達しました。 そもそも、「note」とは、クリエイターが、文章やマンガ、写真、音声を投稿することができ、ユーザーはその ...

5

ボードゲームカフェが1日2回転で儲かるという記事をみつけたので興味を持ち、調べてみました。 まずは、需要がどれくらいあるのか、市場のようすからみていきましょう。 世界最大のボードゲーム市場はドイツで、 ...

-code, ソーシャル
-,

Copyright© BUSINESS HUNTER , 2023 All Rights Reserved Powered by AFFINGER5.