質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

1回答

2793閲覧

画面のタッチした場所にオブジェクトを徐々に移動させる(iTween)

kingdomheartstk

総合スコア6

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2017/05/03 13:54

編集2017/05/04 15:35

###前提・実現したいこと
タッチしたところにオブジェクトを徐々に移動させたい

###発生している問題・エラーメッセージ
タッチしたところに瞬間移動する

###該当のソースコード
C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Touch3 : MonoBehaviour {
private Vector3 screenPoint;
private Vector3 offset;
// Use this for initialization
void Start () {
screenPoint = Camera.main.WorldToScreenPoint(transform.position);
float x = Input.mousePosition.x;
float y = Input.mousePosition.y;
offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(x, y, screenPoint.z));
}

// Update is called once per frame void Update () { if (Input.GetKeyDown (KeyCode.Mouse0)) { float x = Input.mousePosition.x; float y = Input.mousePosition.y; Vector3 currentScreenPoint = new Vector3 (x, y, screenPoint.z); Vector3 currentPosition = Camera.main.ScreenToWorldPoint (currentScreenPoint)+ offset; transform.position = currentPosition; } }

}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

iTween使ってませんが、こんな感じのコードで徐々に移動することが出来ます。

C#

1public class TouchMoveSample : MonoBehaviour 2{ 3 float time; 4 float duration; 5 Vector3 startPosition; 6 Vector3 endPosition; 7 8 void Update() 9 { 10 if (Input.GetKeyDown(KeyCode.Mouse0)) 11 { 12 float x = Input.mousePosition.x; 13 float y = Input.mousePosition.y; 14 Vector3 currentScreenPoint = new Vector3(x, y, 0); 15 Vector3 touchPosition = Camera.main.ScreenToWorldPoint(currentScreenPoint); 16 17 Move(touchPosition, 0.2f); 18 } 19 20 if (time <= duration) 21 { 22 time += Time.deltaTime; 23 var x = Mathf.Lerp(startPosition.x, endPosition.x, time / duration); 24 var y = Mathf.Lerp(startPosition.y, endPosition.y, time / duration); 25 transform.position = new Vector3(x, y); 26 } 27 } 28 29 void Move(Vector3 endPosition, float duration) 30 { 31 time = 0; 32 this.duration = duration; 33 startPosition = transform.position; 34 this.endPosition = endPosition; 35 } 36}

投稿2017/05/19 13:32

kokeiro001

総合スコア62

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問