提示画像ですが武器を投げるとプレイヤーが武器を投げた方向に飛んでいくのですが武器と同じ座標に来た時に子のオブジェクトである青い武器がプレイヤーの子の座標の初期座標つまり一枚目の位置に座標を元に戻す方法が知りたいです。提示コードの////コメント部内部が該当コードです。
※提示画像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}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。