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

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

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

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

Unity3D

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

Q&A

解決済

1回答

7384閲覧

character controllerが床をすり抜けてしまう。

退会済みユーザー

退会済みユーザー

総合スコア0

C#

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

Unity3D

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

0グッド

0クリップ

投稿2017/05/04 03:16

character controllerがうまく作用していないみたいで、オブジェクトが床(plane)をすり抜けてしまいます。
planeのis Triggerはオフになってます。
理由がわかりません。助けてください。
character controllerをつけているオブジェクトの詳細は写真を参考ください。イメージ説明

c#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class PlayerControl : MonoBehaviour { 6 private CharacterController playerController; 7 public float MOVESPEED; 8 public float ROTATESPEED; 9 public float GRAVITY; 10 private float move_z,move_x,move_y; 11 private Animator playerAnimator; 12 private float moveforward; 13 private GameObject speakbutton; 14 15 public enum CLOSEST 16 { 17 NONE = -1, 18 FRIEND = 0, 19 X, 20 PLAYER1, 21 }; 22 23 public CLOSEST closest; 24 25 // Use this for initialization 26 void Start () { 27 playerController = this.GetComponent<CharacterController> (); 28 playerAnimator = this.GetComponent<Animator> (); 29 speakbutton = GameObject.FindGameObjectWithTag ("SpeakButton"); 30 speakbutton.gameObject.SetActive (false); 31 } 32 33 // Update is called once per frame 34 void Update () { 35 if (Input.GetAxis ("Vertical") > 0) { //上矢印で前進量を取得. 36 move_z = Input.GetAxis ("Vertical"); 37 } else { //上矢印が押されてなかったら動かない. 38 move_z = 0.0f; 39 } 40 move_x = Input.GetAxis ("Horizontal"); //回転量を取得. 41 move_z *= MOVESPEED * Time.deltaTime; //速さをかける. 42 43 if (!playerController.isGrounded) { //接地してなかったら落ちる. 44 move_y -= GRAVITY * Time.deltaTime; 45 } 46 if (this.transform.position.y < -25.0f) //落ちすぎてたら-25で止める. 47 move_y = 0.0f; 48 49 //相対的に前へ. 50 this.transform.Translate (new Vector3 (0, move_y, move_z)); 51 //相対回転. 52 this.transform.Rotate(new Vector3 (0, 1, 0), move_x * Time.deltaTime * ROTATESPEED); 53 playerAnimator.SetBool ("run", move_z > 0); //前に進んでいたらrunアニメーションをtrue. 54 } 55 56 void displaySpeakButton(){ 57 speakbutton.gameObject.SetActive (true); 58 } 59 60 void hideSpeakButton(){ 61 speakbutton.gameObject.SetActive (false); 62 } 63 64 void OnTriggerStay(Collider other){ 65 GameObject other_go = other.gameObject; 66 if (this.isFront (other_go)) { 67 if (other_go.CompareTag ("Friend")) { 68 closest = CLOSEST.FRIEND; 69 displaySpeakButton (); 70 } else if (other_go.CompareTag ("X")) { 71 closest = CLOSEST.X; 72 displaySpeakButton (); 73 } else if (other_go.CompareTag ("Player1")) { 74 closest = CLOSEST.PLAYER1; 75 displaySpeakButton (); 76 } else { 77 closest = CLOSEST.NONE; 78 hideSpeakButton (); 79 } 80 } else { 81 closest = CLOSEST.NONE; 82 hideSpeakButton (); 83 } 84 } 85 86 void OnTriggerExit(){ 87 closest = CLOSEST.NONE; 88 } 89 90 bool isFront(GameObject go){ 91 bool ret = false; 92 do{ 93 //向いてる方向を取得. 94 Vector3 heading = this.transform.TransformDirection(Vector3.forward); 95 //ゲームオブジェクトの方向を取得. 96 Vector3 to_go = go.transform.position - this.transform.position; 97 heading.y = 0.0f; 98 to_go.y = 0.0f; 99 heading.Normalize(); 100 to_go.Normalize(); 101 float dotProduct = Vector3.Dot(heading, to_go); 102 if(dotProduct < Mathf.Cos(45.0f)){ 103 //2つの方向が45度以上なら抜ける. 104 break; 105 } 106 ret = true; 107 }while(false); 108 return(ret); 109 } 110} 111

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

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

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

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

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

guest

回答1

0

ベストアンサー

移動をthis.transform.translateで行っていたのが原因でした。

c#

1this.transform.Translate (new Vector3 (0, move_y, move_z));

の部分を

c#

1playerController.Move(new Vector3(0,move_y,0)); 2playerController.Move(new Vector3(this.transform.forward*move_z);

としたところうまく動作しました。

投稿2017/05/04 05:13

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.49%

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

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

質問する

関連した質問