Когда мой игровой объект назначает позицию одного игрового объекта другому игровому объекту, он запускается в некотором направлении без ввода - PullRequest
0 голосов
/ 07 марта 2020

я серьезно сбит с толку,

мой игровой объект в порядке, а ближайший игровой объект тоже в порядке. но во-вторых, я говорю

"gameObject1.transform.position = gameObject2.transform.position", он выстреливает в космос

я чувствую, что мой контроллер не такой сложный, поэтому я собираюсь поставить все здесь для ясности

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

public class hero_controller : MonoBehaviour
{

/// <summary>
///  temporary
/// </summary>
GameObject current_cart;


public float rotation_percentage = 60;
public float rotation_factor = 5;
private float vertical_motion = 0;
private float horizontal_motion = 0;

public Vector3 adjustable_look_at_offset;

public Vector3 offset_location;

public float push_or_pull_force = 0;
public float camera_location_scalar = 3;

public float lerp_factor = 0.125f;

public Vector3 cart_orientation;

private Vector2 rotation_lock;

private float orientation_switch;


_hero_object hero;

// Start is called before the first frame update
void Start()
{
    hero = GameObject.Find("ScriptManager").GetComponent<create_scene>().get_hero();
    offset_location = new Vector3(0, 3.25f, -3);
    adjustable_look_at_offset = new Vector3(0, 1.5F, 0);

    // initially hero will have a cart temporarily
    current_cart = GameObject.Find("pushcart(Clone)");

    rotation_lock.x = current_cart.transform.rotation.x;
    rotation_lock.y = current_cart.transform.rotation.z;


}

// Update is called once per frame
void Update()
{
    vertical_motion = 0;
    horizontal_motion = 0;

    check_cart_ownership();

    if (hero.request_cart_status())
    {
        hero.is_pushing(observation: true);
        lock_the_shopping_cart();
        //grab_cart();
    }
    else {
        hero.is_pushing(observation: false);
    }

    if (Input.GetKey(KeyCode.UpArrow)) {

        orientation_switch = 1;
        vertical_motion = 1;
    }
    else if (Input.GetKey(KeyCode.DownArrow)) {
        orientation_switch = -1;
        vertical_motion = -1;
    }


    if (Input.GetKey(KeyCode.LeftArrow)) {
        horizontal_motion = -1;
    }
    else if (Input.GetKey(KeyCode.RightArrow)) {
        horizontal_motion = 1;
    }



    if (!Input.GetKey(KeyCode.UpArrow) && !Input.GetKey(KeyCode.DownArrow)) {

        if (Input.GetKey(KeyCode.A))
        {
            hero.should_turn(decision: true);
            hero.turn(this_direction: turning_direction.left);
        }
        else if (Input.GetKey(KeyCode.F))
        {
            hero.should_turn(decision: true);
            hero.turn(this_direction: turning_direction.right);
        }
        else
        {
            hero.should_turn(decision: false);
        }

    }
    else {

        if (Input.GetKey(KeyCode.A))
        {
            hero.prefab.transform.Rotate(0, -rotation_factor  * orientation_switch, 0);
        }
        else if (Input.GetKey(KeyCode.F))
        {
            hero.prefab.transform.Rotate(0, rotation_factor  * orientation_switch , 0);
        }


    }

    if (Input.GetKey(KeyCode.T)) {
        hero.throw_merch(should_throw: true); ;
    }

    hero.move(horizontal: horizontal_motion, vertical: vertical_motion);

    set_location(for_the: hero.camera);
    set_location(for_the: hero.look_at_target, is_camera: false);

    hero.camera.transform.LookAt(hero.look_at_target.transform);

}

private void set_location(GameObject for_the, bool is_camera = true) {

    float current_rotation;

    if ((int)hero.prefab.transform.localEulerAngles.y > 0) {
        current_rotation = Mathf.Floor((int)hero.prefab.transform.localEulerAngles.y);
    }
    else {
        current_rotation = Mathf.Floor(360 + (int)hero.prefab.transform.rotation.y);
    }


    if (is_camera)
    {
        offset_location.x = hero.prefab.transform.position.x - (camera_location_scalar * Mathf.Sin(current_rotation / rotation_percentage));
        offset_location.z = hero.prefab.transform.position.z - (camera_location_scalar * Mathf.Cos(current_rotation / rotation_percentage));
    }
    else {
        offset_location.x  =  adjustable_look_at_offset.x + hero.prefab.transform.position.x + (camera_location_scalar * Mathf.Sin(current_rotation / rotation_percentage));
        offset_location.y  =  adjustable_look_at_offset.y;
        offset_location.z  =  adjustable_look_at_offset.z + hero.prefab.transform.position.z + (camera_location_scalar * Mathf.Cos(current_rotation / rotation_percentage));
    }


    for_the.transform.position = Vector3.Slerp(for_the.transform.position, offset_location, lerp_factor);

}

public void stop_throwing() {
    hero.stop_throwing_merch();
}

public void grab_cart() {
}


/////////////////////////
///// offending code ////
/////////////////////////

public void lock_the_shopping_cart() {
    current_cart.transform.position = hero.prefab.transform.position;
    current_cart.transform.Rotate(rotation_lock.x, current_cart.transform.rotation.y, rotation_lock.y);
}

public void check_cart_ownership() {
    hero.set_cart_status(cart_status: false);

    //for (int i = 0; i < hero.hand.transform.childCount; i++)
    //{
    //    if (hero.hand.transform.GetChild(i).tag == "shopping_cart")
    //    {
    hero.set_cart_status(cart_status: true);
    //    }
    //}

}
}

я не хочу блокировать позицию, я пытаюсь придумать самое логичное в реальной жизни,

цель состоит в том, чтобы вы sh и потяните корзину для покупок, чтобы она не двигалась руками.

в реальной жизни вы берете корзину (родительские отношения), и корзина движется вместе с вами

, которая не работает так далеко, потому что хотя я могу компенсировать вращение рук ... мне также придется компенсировать root вариаций движения ... что я могу сделать, но я уверен, что это больше работы, чем необходимо

Похоже, был бы существующий метод для того, что я делаю

...