Unityゲーム制作 スマホタッチによる移動処理 エラーの解決法がわからないです

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら

トピックに返信する


答えを正確にご入力ください。答えられるかどうかでスパムボットか否かを判定します。

BBCode: ON
[img]: ON
[flash]: OFF
[url]: ON
スマイリー: OFF

トピックのレビュー
   

展開ビュー トピックのレビュー: Unityゲーム制作 スマホタッチによる移動処理 エラーの解決法がわからないです

Re: Unityゲーム制作 スマホタッチによる移動処理 エラーの解決法がわからないです

#4

by あたっしゅ » 1年前

東上☆海美☆「
#3> このエラーに対してはどのように対応すればよいでしょうか?
#3> それともそもそも「int leftTouchInput;」を入れたのが間違いでしょうか?

他の場所では、

#1> Vector2 leftTouchInput = new Vector2();

とあるので、int 型ではなく、.x と .y がある Vector2 型ではないのでしょうか ?


#1> 自分はある本を参考にUnityでのスマホゲーム製作を始めました。
#1>プログラミングをするのはその本が初めてで全くの初心者です。

『C/C++ は、わかってるけど、C#は、はじめて』じゃなくて、
『プログラミング』そのものが、はじめて、なんですか ?
それで、『int leftTouchInput; 入れた』とか言っているのであれば、
いきなり、スマホ・アプリを作るのは、無謀のようです。
もっとやさしいものをやって、基礎力をつけるべきです。

ちなみに、参考にした本の著者、題名、出版社を教えてほしい。

Re: Unityゲーム制作 スマホタッチによる移動処理 エラーの解決法がわからないです

#3

by rrrr » 1年前

回答ありがとうございます!

00>(前略)
01> void FixedUpdate()
02> {
   int leftTouchInput;
03> if (isAttack == false)
04> {
05> Vector3 input = new Vector3();
06> Vector3 move = new Vector3();
07> if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
08> {
09> input = new Vector3(leftTouchInput.x, 0, leftTouchInput.y);
10> move = input.normalized * 2f;
11> }
12> else
13>(後略)

すいません、その宣言というのもわからない初心者なもので、ネットで調べてとりあえず上のように「int leftTouchInput;」というのを2と3の間に当てはめてみましたが、以下のエラーが出てしまいました。

CS1061: 'int' does not contain a definition for 'x' and no accessible extension method 'x' accepting a first argument of type 'int' could be found (are you missing a using directive or an assembly reference?)

CS1061: 'int' does not contain a definition for 'y' and no accessible extension method 'y' accepting a first argument of type 'int' could be found (are you missing a using directive or an assembly reference?)

CS0165: Use of unassigned local variable 'leftTouchInput'


このエラーに対してはどのように対応すればよいでしょうか?それともそもそも「int leftTouchInput;」を入れたのが間違いでしょうか?

Re: Unityゲーム制作 スマホタッチによる移動処理 エラーの解決法がわからないです

#2

by あたっしゅ » 1年前

東上☆海美☆「
>The name 'leftTouchInput' does not exist in the current context

『この文脈で、leftTouchInput が存在しない』とエラってるみみ。


00>(前略)
01> void FixedUpdate()
02> {
03> if (isAttack == false)
04> {
05> Vector3 input = new Vector3();
06> Vector3 move = new Vector3();
07> if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
08> {
09> input = new Vector3(leftTouchInput.x, 0, leftTouchInput.y);
10> move = input.normalized * 2f;
11> }
12> else
13>(後略)

void FixedUpdate() の中で、leftTouchInput が宣言されていないのに、
09 行で、input = new Vector3(leftTouchInput.x, 0, leftTouchInput.y);
と使っているからみみ。

Unityゲーム制作 スマホタッチによる移動処理 エラーの解決法がわからないです

#1

by rrrr » 1年前

はじめまして。自分はある本を参考にUnityでのスマホゲーム製作を始めました。プログラミングをするのはその本が初めてで全くの初心者です。

現在はプレイヤーの攻撃やジャンプ、移動の処理をしてそれをスマホにビルドして動かしてみようかと思っているところです。

「void FixedUpdate()」から「攻撃ボタンクリックコールバック」までで一部のコードを、本を参考に追加記入してみたところ以下のエラーメッセージが出ました。

cs(144,37): error CS0103: The name 'leftTouchInput' does not exist in the current context
cs(144,58): error CS0103: The name 'leftTouchInput' does not exist in the current context

本を何度も確認しましたが誤字脱字などは見当たらなかったのですが、どのようにエラーを解決したらよいでしょうか?

どこまで載せたらいいのかわからなかったので、作業が止まっているところまでコードを載せておきます。長くて見にくいとは思いますが、よろしくお願いします。


コード:

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

public class PlayerController : MonoBehaviour
{
    //攻撃判定用オブジェクト.
    [SerializeField] GameObject attackHit = null;
    //設置判定用Collidercall
    [SerializeField] ColliderCallReceiver footColliderCall = null;
    //ジャンプ力
    [SerializeField] float jumpPower = 20f;
    //アニメーター.
    Animator animator = null;
    //リジッドボディ.
    Rigidbody rigid = null;
    //攻撃アニメーション中フラグ.
    bool isAttack = false;
    //接地フラグ.
    bool isGround = false;

    //PCキー横方向入力.
    float horizontalKeyInput = 0;
    //PCキー縦方向入力.
    float verticalKeyInput = 0;

    //Start is called before the first frame update
    void Start()
    {
        //Animatorを取得し保管.
        animator = GetComponent<Animator>();
        //Rigidbodyの取得.
        rigid = GetComponent<Rigidbody>();
        //攻撃判定用オブジェクトを非表示に.
        attackHit.SetActive(false);

        //FootSphereのイベント登録.
        footColliderCall.TriggerEnterEvent.AddListener(OnFootTriggerEnter);
        footColliderCall.TriggerExitEvent.AddListener(OnFootTriggerExit);
    }

    //Update is called once per frame
    bool isTouch = false;

    void Update()
    {
        if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
        {
            //スマホタッチ操作.
            //タッチしている指の数が0より多い.
            if (Input.touchCount > 0)
            {
                isTouch = true;
                //タッチ情報をすべて取得.
                Touch[] touches = Input.touches;
                //全部のタッチを繰り返して判定.
                foreach (var touch in touches)
                {
                    //左半分タッチスタート位置.
                    Vector2 leftStartTouch = new Vector2();
                    //左半分タッチ入力.
                    Vector2 leftTouchInput = new Vector2();

                    bool isLeftTouch = false;
                    bool isRightTouch = false;
                    //タッチ位置のX軸方向がスクリーンの左側.
                    if (touch.position.x > 0 && touch.position.x < Screen.width / 2)
                    {
                        isLeftTouch = true;
                    }
                    //タッチ位置のX軸方向がスクリーンの右側.
                    else if (touch.position.x > Screen.width / 2 && touch.position.x < Screen.width)
                    {
                        isRightTouch = true; ;
                    }

                    //左タッチ.
                    if (isLeftTouch == true)
                    {
                        //タッチ開始.
                        if (touch.phase == TouchPhase.Began)
                        {
                            //開始位置を保管.
                            leftStartTouch = touch.position;
                        }
                        //タッチ中.
                        else if (touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Stationary)
                        {
                            //現在の位置を随時保管.
                            Vector2 position = touch.position;
                            //移動用の方向を保管.
                            leftTouchInput = position - leftStartTouch;
                        }
                        //タッチ終了.
                        else if (touch.phase == TouchPhase.Ended)
                        {
                            leftTouchInput = Vector2.zero;
                        }
                    }

                    //右タッチ.
                    if (isRightTouch == true)
                    {
                        //右半分をタッチした際の処理.
                    }
                }
            }
            else
            {
                isTouch = false;
            }
        }
        else
        {
            //PCキー入力取得.
            horizontalKeyInput = Input.GetAxis("Horizontal");
            verticalKeyInput = Input.GetAxis("Vertical");
        }
        //プレイヤーの向きを調整.
        bool isKeyInput = (horizontalKeyInput != 0 || verticalKeyInput != 0 );
        if (isKeyInput == true && isAttack == false) 
        {
            bool currentIsRun = animator.GetBool("isRun");
            if (currentIsRun == false) animator.SetBool("isRun", true);
            Vector3 dir = rigid.velocity.normalized;
            dir.y = 0;
            this.transform.forward = dir;
        }
        else
        {
            bool currentIsRun = animator.GetBool("isRun");
            if (currentIsRun == true) animator.SetBool("isRun", false);
        }
    }

    void FixedUpdate()
    {
        if (isAttack == false)
        {
            Vector3 input = new Vector3();
            Vector3 move = new Vector3();
            if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
            {
                input = new Vector3(leftTouchInput.x, 0, leftTouchInput.y);
                move = input.normalized * 2f;
            }
            else
            {
                input = new Vector3(horizontalKeyInput, 0, verticalKeyInput);
                move = input.normalized * 2f;
            }

            Vector3 cameraMove = Camera.main.gameObject.transform.rotation * move;
            cameraMove.y = 0;
            Vector3 currentRigidVelocity = rigid.velocity;
            currentRigidVelocity.y = 0;

            rigid.AddForce(cameraMove - currentRigidVelocity, ForceMode.VelocityChange);
        }
    }

    //-----------------------------------------------------
    ///<summary>
    ///攻撃ボタンクリックコールバック.
    ///</summary>
    //-----------------------------------------------------
    public void OnAttackButtonClicked()
    {
        if (isAttack == false)
        {
            //AnimationのisAttackトリガーを起動.
            animator.SetTrigger("isAttack");
            //攻撃開始.
            isAttack = true;
        }
    }

    //------------------------------------------------------
    ///<summary>
    ///ジャンプボタンクリックコールバック.
    ///</summary>
    //------------------------------------------------------
    public void OnJumpButtonClicked()
    {
        if (isGround == true)
        {
            rigid.AddForce(Vector3.up * jumpPower, ForceMode.Impulse);
        }
    }

    //------------------------------------------------------
    ///<summary>
    ///FootSphereトリガーエンターコール.
    /// </summary>
    /// <param name="col">侵入したコライダー.</param>
    //------------------------------------------------------
    void OnFootTriggerEnter(Collider col)
    {
        if (col.gameObject.tag == "Ground")
        {
            isGround = true;
            animator.SetBool("isGround", true);
        }

    }

    //------------------------------------------------------
    ///<summary>
    ///FootSphereトリガーイグジットコール.
    /// </summary>
    /// <param name="col">侵入したコライダー.</param>
    //------------------------------------------------------
    void OnFootTriggerExit(Collider col)
    {
        if (col.gameObject.tag == "Ground")
        {
            isGround = false;
            animator.SetBool("isGround", false);
        }
    }

    //------------------------------------------------------
    ///<summary>
    ///攻撃アニメーションHitイベントコール.
    /// </summary>
    //------------------------------------------------------
    void Anim_AttackHit()
    {
        Debug.Log("Hit");
        //攻撃判定用オブジェクトを表示.
        attackHit.SetActive(true);
    }

    //------------------------------------------------------
    ///<summary>
    ///攻撃アニメーション終了イベントコール.
    /// </summary>
    //------------------------------------------------------
    void Anim_AttackEnd()
    {
        Debug.Log("End");
        //攻撃判定用オブジェクトを非表示に.
        attackHit.SetActive(false);
        //攻撃終了.
        isAttack = false;    
    }
}

ページトップ