首页 > 解决方案 > Unity 3d对象保持问题我该如何解决?

问题描述

如果角色在没有鼠标移动对象的情况下移动而不被玩家跟随,我该如何解决此事件。鼠标应该移动以使对象跟随,我该如何解决?这是我的脚本如何工作https://youtu.be/f5P5epEA3-w

我的代码:

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

    public class ObjectHoldRay : MonoBehaviour
    {
        public Transform player;
        public Transform Kamera;
        private Camera playerCam;
        public float throwForce = 10;
        bool hasPlayer = false;
        bool beingCarried = false;
        public AudioClip[] soundToPlay;
        public int dmg;
        private bool touched = false;
        public float mesafe;
        private Ray playerAim;

        void Start()
        {
        }

        void Update()
        {
            playerCam = Camera.main;
            Ray playerAim = playerCam.GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
            RaycastHit hit;
            if (Physics.Raycast(playerAim, out hit, mesafe))
            {
                if (hit.collider.gameObject.tag == "tasinabilir")
                {
                    hasPlayer = true;
                }
                else
                {
                    hasPlayer = false;
                }
                if (hasPlayer && Input.GetMouseButtonDown(0))
                {
                    GetComponent<Rigidbody>().isKinematic = true;
                    transform.parent = Kamera;
                    beingCarried = true;
                }
                if (beingCarried)
                {
                    if (touched)
                    {
                        GetComponent<Rigidbody>().isKinematic = false;
                        transform.parent = null;
                        beingCarried = false;
                        touched = false;
                    }
                    if (Input.GetAxis("Mouse ScrollWheel") > 0f) // forward
                    {
                        Debug.Log("İLeri");
                    }
                    if (Input.GetAxis("Mouse ScrollWheel") < 0f) // geri
                    {
                        Debug.Log("Geri");
                    }
                    //if (Input.GetMouseButtonDown(0))
                    //{
                    //  GetComponent<Rigidbody>().isKinematic = false;
                    //transform.parent = null;
                    //beingCarried = false;
                    //GetComponent<Rigidbody>().AddForce(playerCam.forward * throwForce);
                    //}
                    if (Input.GetMouseButtonDown(1))
                    {
                        GetComponent<Rigidbody>().isKinematic = false;
                        transform.parent = null;
                        beingCarried = false;
                    }
                }
            }
        }

        void OnTriggerEnter()
        {
            if (beingCarried)
            {
                touched = true;
            }
        }
    }

标签: unity3d

解决方案


您是否尝试在拾取后将您的可移动对象作为玩家的孩子而不是相机?

transform.parent = player;

推荐阅读