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

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

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

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

Unity3D

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

Q&A

2回答

551閲覧

[Unity3D] 子のオブジェクトの座標を初期座標に書き換える方法が知りたい。

退会済みユーザー

退会済みユーザー

総合スコア0

C#

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

Unity3D

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

0グッド

0クリップ

投稿2022/04/12 05:59

編集2022/04/13 05:51

提示画像ですが武器を投げるとプレイヤーが武器を投げた方向に飛んでいくのですが武器と同じ座標に来た時に子のオブジェクトである青い武器がプレイヤーの子の座標の初期座標つまり一枚目の位置に座標を元に戻す方法が知りたいです。提示コードの////コメント部内部が該当コードです。

※提示画像1武器を投げる前 提示画像2 武器を投げた後

イメージ説明
イメージ説明

Sword.cs

cs

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5namespace Player 6{ 7 8 /* 9 * ################################################# 10 * 武器 ソード 11 * ################################################# 12 */ 13 14 15 16 public class Player_Sword : MonoBehaviour 17 { 18 Animator animator; 19 bool isWarpShift = false; 20 Transform parent; 21 Vector3 position; 22 bool isMoveCharacter = false; 23 float shiftDistance = 0; 24 25 float shiftSpeed = 10; //シフト速度 26 float shiftRange = 10; //ソフト距離 27 28 29 30 31 32 public bool getIsWarpShift() 33 { 34 return isMoveCharacter; 35 } 36 37 38 // Start is called before the first frame update 39 void Start() 40 { 41 42 parent = transform.parent; 43////////////////////////////////////////////////////////////////////////// 44 position = transform.localPosition; 45////////////////////////////////////////////////////////////////////////// 46 animator = GetComponent<Animator>(); 47 } 48 49 // Update is called once per frame 50 void Update() 51 { 52 if ((animator.GetCurrentAnimatorStateInfo(0).IsName("attack") == false) && (animator.GetCurrentAnimatorStateInfo(0).IsName("sword_shoot") == false) ) 53 { 54 if (Input.GetButtonDown("R1") == true) 55 { 56 animator.SetTrigger("Attack"); 57 } 58 59 if (Input.GetButtonDown("R2") == true) 60 { 61 animator.SetTrigger("Warp"); 62 } 63 } 64 65 66 67 if(animator.GetCurrentAnimatorStateInfo(0).IsName("idle") == true) 68 { 69 isWarpShift = false; 70 } 71 72 // 投げる 73 if(isWarpShift == true) 74 { 75 if (isMoveCharacter == false) 76 { 77 transform.Translate(Vector3.forward * 10 * Time.deltaTime); 78 79 shiftDistance += 10 * Time.deltaTime; 80 } 81 82 if (shiftDistance > 10) 83 { 84 transform.parent = null; 85 isMoveCharacter = true; 86 shiftDistance = 0; 87 } 88 89 } 90 91 } 92 93 94 /*########################################## シフト移動開始 イベント ##########################################*/ 95 void OnWarp_Event() 96 { 97 animator.speed = 0; 98 isWarpShift = true; 99 } 100 101 /*########################################## アニメーション終了 ##########################################*/ 102 public void WarpShiftEnd() 103 { 104 animator.speed = 1; 105 animator.SetTrigger("WarpEnd"); 106 isMoveCharacter = false; 107////////////////////////////////////////////////////////////////////////// 108 transform.localPosition = position; 109 transform.parent = parent; 110////////////////////////////////////////////////////////////////////////// 111 } 112 113 114 /*########################################## Shift 距離 ##########################################*/ 115 public float getShiftRange() 116 { 117 return shiftRange; 118 } 119 } 120} 121
Contorl.cs

cs

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5namespace Player 6{ 7 8 /* 9 * ################################################# 10 * プレイヤーコントロール 11 * ################################################# 12 */ 13 14 public class Player_Control : MonoBehaviour 15 { 16 17 [SerializeField] const float walkSpeed = 10; //移動速度 18 [SerializeField] const float fallSpeed = 15; //落下速度 19 [SerializeField] GameObject weapon; //武器 20 21 Vector3 moveSpeed; 22 bool isJump = false; 23 CharacterController controller; 24 Animator animator; 25 float distance = 0; 26 27 28 void Start() 29 { 30 controller = GetComponent<CharacterController>(); 31 animator = GetComponent<Animator>(); 32 } 33 34 void Update() 35 { 36 Move(); 37 38 39 if (weapon.GetComponent<Player_Sword>().getIsWarpShift() == true) 40 { 41 controller.Move(transform.forward * 10 * Time.deltaTime); 42 distance += 10 * Time.deltaTime; 43 if(distance > weapon.GetComponent<Player_Sword>().getShiftRange()) 44 { 45 weapon.GetComponent<Player_Sword>().WarpShiftEnd(); 46 distance = 0; 47 48 } 49 50 } 51 52 } 53 54 55 void FixedUpdate() 56 { 57 58 } 59 60 float shitRange = 0; 61 62 /*########################################## Late Update ##########################################*/ 63 void LateUpdate() 64 { 65 66 } 67 68 69 70 /*########################################## 移動 ##########################################*/ 71 void Move() 72 { 73 float inputHorizontal = Input.GetAxis("Left_Horizontal"); 74 float inputVertical = Input.GetAxis("Left_Vertical"); 75 76 Quaternion horizontalRotation = Quaternion.AngleAxis(Camera.main.transform.eulerAngles.y,Vector3.up); 77 Vector3 velocity = horizontalRotation * new Vector3(inputHorizontal, 0, inputVertical * -1).normalized; 78 79 if (velocity.magnitude > 0.3f) 80 { 81 if ((weapon.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsName("attack") == false) && (weapon.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsName("sword_shoot") == false)) 82 { 83 transform.rotation = Quaternion.LookRotation(velocity, transform.up); 84 } 85 86 moveSpeed.x = (velocity * walkSpeed).x; 87 moveSpeed.z = (velocity * walkSpeed).z; 88 } 89 else 90 { 91 moveSpeed.x = 0; 92 moveSpeed.z = 0; 93 } 94 95 96 Jump(); 97 98 99 // 攻撃していない時移動可能 100 if( (weapon.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsName("attack") == false) && (weapon.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsName("sword_shoot") == false) ) 101 { 102 Vector3 move = Vector3.Scale(moveSpeed, new Vector3(1, 0, 1)); 103 controller.Move(moveSpeed * Time.deltaTime); //移動 104 } 105 } 106 107 /*########################################## ジャンプ ##########################################*/ 108 void Jump() 109 { 110 if( (Input.GetButtonDown("Cross") == true) && (controller.isGrounded == true) ) 111 { 112 isJump = true; 113 moveSpeed.y = 300; 114 } 115 else 116 { 117 moveSpeed.y = -20; 118 } 119 } 120 } 121}

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

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

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

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

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

guest

回答2

0

chatgpt3.5
このUnityプロジェクトでは、プレイヤーの剣とその投げるメカニズムを扱うC#スクリプトがあるようです。目標は、剣(プレイヤーの子オブジェクト)が投げられてプレイヤーの位置に戻ったときに、剣の位置を元の初期位置にリセットすることです。

これを実現するには、Player_Sword.csスクリプトを変更する必要があります。具体的には、WarpShiftEnd()メソッドを修正して、剣がプレイヤーに戻ったときにその位置を元の初期位置にリセットします。以下に更新されたスクリプトを示します:

csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Player
{
public class Player_Sword : MonoBehaviour
{
// 他の変数とメソッド...

Vector3 initialLocalPosition; // 剣の初期ローカル位置を保存する変数 // Startは最初のフレームの前に呼び出される void Start() { parent = transform.parent; animator = GetComponent<Animator>(); // 剣の初期ローカル位置を保存する initialLocalPosition = transform.localPosition; } // Updateは1フレームごとに呼び出される void Update() { // 他の更新ロジック... } /*########################################## アニメーション終了 ##########################################*/ public void WarpShiftEnd() { animator.speed = 1; animator.SetTrigger("WarpEnd"); isMoveCharacter = false; // 剣の位置を初期ローカル位置にリセットする transform.localPosition = initialLocalPosition; transform.parent = parent; } // 他のメソッド... }

}
これらの変更により、WarpShiftEnd()メソッドは、剣が投げられてプレイヤーに戻ったときに、剣の位置をinitialLocalPosition変数に保存された初期位置にリセットします。

なお、このコードでは剣の初期位置が親オブジェクト(プレイヤー)に対して相対的であると仮定しています。もし剣をワールド内の絶対的な位置にリセットしたい場合は、transform.localPositionの代わりにtransform.positionを使用する必要があります。

投稿2023/07/26 10:20

a7rhj0dvnk

総合スコア28

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

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

0

chatgpt3.5
このUnityプロジェクトでは、プレイヤーの剣とその投げるメカニズムを扱うC#スクリプトがあるようです。目標は、剣(プレイヤーの子オブジェクト)が投げられてプレイヤーの位置に戻ったときに、剣の位置を元の初期位置にリセットすることです。

これを実現するには、Player_Sword.csスクリプトを変更する必要があります。具体的には、WarpShiftEnd()メソッドを修正して、剣がプレイヤーに戻ったときにその位置を元の初期位置にリセットします。以下に更新されたスクリプトを示します:

csharp

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5namespace Player 6{ 7 public class Player_Sword : MonoBehaviour 8 { 9 // 他の変数とメソッド... 10 11 Vector3 initialLocalPosition; // 剣の初期ローカル位置を保存する変数 12 13 // Startは最初のフレームの前に呼び出される 14 void Start() 15 { 16 parent = transform.parent; 17 animator = GetComponent<Animator>(); 18 19 // 剣の初期ローカル位置を保存する 20 initialLocalPosition = transform.localPosition; 21 } 22 23 // Updateは1フレームごとに呼び出される 24 void Update() 25 { 26 // 他の更新ロジック... 27 } 28 29 /*########################################## アニメーション終了 ##########################################*/ 30 public void WarpShiftEnd() 31 { 32 animator.speed = 1; 33 animator.SetTrigger("WarpEnd"); 34 isMoveCharacter = false; 35 36 // 剣の位置を初期ローカル位置にリセットする 37 transform.localPosition = initialLocalPosition; 38 transform.parent = parent; 39 } 40 41 // 他のメソッド... 42 } 43}

これらの変更により、WarpShiftEnd()メソッドは、剣が投げられてプレイヤーに戻ったときに、剣の位置をinitialLocalPosition変数に保存された初期位置にリセットします。

なお、このコードでは剣の初期位置が親オブジェクト(プレイヤー)に対して相対的であると仮定しています。もし剣をワールド内の絶対的な位置にリセットしたい場合は、transform.localPositionの代わりにtransform.positionを使用する必要があります。

投稿2023/07/26 10:19

a7rhj0dvnk

総合スコア28

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問