

エラーを吐いてしまい動きません、エラーの内容は以下の通りです。

[code]

    error CS0120: An object reference is required for the non-static field, method, or property 'raycast.movePosition'
    
[/code]

どうすれば動きますか？また動いたら正確に動くでしょうか。

[code]

    public class raycast : MonoBehaviour
    {
        Vector3 movePosition; 
    
        void Start()
        {
            movePosition = moveRandomPosition();  
        }
        public class Test : MonoBehaviour
        {
    
            void Update()
            {
                Ray ray = movePosition;
    
                RaycastHit hit;
                if (Physics.Raycast(ray, out hit, 10.0f))
                {
                    Debug.Log(hit.collider.gameObject.transform.position);
                }
                Debug.DrawRay(ray.origin, ray.direction * 10, Color.red, 5);
                movePosition = moveRandomPosition();
            }
        }
        private Vector3 moveRandomPosition() 
        {
            Vector3 randomPosi = new Vector3(Random.Range(-7, 7), Random.Range(0, 0), Random.Range(-7, 7));
            return randomPosi;
        }
    
[/code]


CC BY-SA 4.0
2022-03-19T09:16:46.230
87914
2022-03-20T11:06:39.387
2022-03-19T09:30:04.990
3060
51886
question
unity3d
ランダムな場所をrayであるかどうかを確認させたい


こんな感じでうろうろさせたいのであれば、

[![Wonder.gif](https://i.stack.imgur.com/jq4mD.gif)](https://i.stack.imgur.com/jq4mD.gif)

こういうコードでできます。

[code]

    using System.Collections;
    using UnityEngine;
    
    public class RayTest : MonoBehaviour
    {
        [SerializeField] float _moveSpeed = 1.0f;
        [SerializeField] float _rayLength = 5.0f;
        Vector3 _destination = default;
    
        void Start()
        {
            MoveToRandomDestination();
        }
    
        void Update()
        {
            Ray ray = new Ray(this.transform.position, _destination - this.transform.position);
            Debug.DrawRay(ray.origin, ray.direction * _rayLength, Color.red);
    
            if (Physics.Raycast(ray, out RaycastHit hit, _rayLength))
            {
                Debug.Log($"{hit.collider.name} を検出した。");
                MoveToRandomDestination();
            }
        }
    
        void MoveToRandomDestination()
        {
            StopAllCoroutines();
            _destination =
                new Vector3(Random.Range(-7, 7), 0, Random.Range(-7, 7));
            Debug.DrawLine(_destination, _destination + Vector3.up, Color.green, 3f);
            StartCoroutine(MoveToDestination());
        }
    
        IEnumerator MoveToDestination()
        {
            Vector3 direction = _destination - this.transform.position;
    
            while (Vector3.Distance(this.transform.position, _destination) > _rayLength)
            {
                this.transform.Translate(direction.normalized * _moveSpeed * Time.deltaTime);
                yield return null;
            }
    
            Debug.Log("目的地に到着した");
            MoveToRandomDestination();
        }
    }
    
[/code]

[unitypackage](https://www.dropbox.com/s/jhal6guad5i9fc4/87914.unitypackage?dl=1)
も置いておきます。


CC BY-SA 4.0
2022-03-20T11:06:39.387
87935
2022-03-20T11:06:39.387
48297
87914
answer