Unity 3D объект удержания проблемы, как я могу это исправить? - PullRequest
0 голосов
/ 01 ноября 2018

Как я могу исправить это событие, если персонаж двигается без мыши, чтобы переместить объект, а затем игрок остается там, где он находится. Мышь должна двигаться, чтобы объект следовал, как мне решить это? Вот мой скрипт, как работает 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 голосов
/ 01 ноября 2018

Пытались ли вы сделать свой подвижный объект дочерним игроком вместо камеры после выбора?

transform.parent = player;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...