Unity 3d对象保存问题我该如何解决?

时间:2018-11-01 17:27:39

标签: unity3d

如果角色在没有鼠标移动对象的情况下移动,而跟随着玩家停留在原处,则该如何解决此事件。鼠标应移动以跟随对象,我该如何解决? 这是我的脚本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;
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

在拾取后,您是否尝试过将可移动对象作为播放器的子代而不是照相机?

transform.parent = player;
相关问题