снаряд единства появляется, но не набирает скорость? - PullRequest
0 голосов
/ 15 февраля 2019

Это мой код, кто-нибудь знает, или кто-нибудь может определить, почему мой снаряд остается неподвижным после его появления?снаряд - это сборная оболочка, заранее спасибо за помощь.

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

public class TankBehaviour : MonoBehaviour
{

    public GameObject shellPrefab;
    public Transform fireTransform;
    private bool isFired = false;


    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        float x = Input.GetAxis("Horizontal");
        float y = Input.GetAxis("Vertical");

        transform.position += transform.forward * y;
        transform.Rotate(0, x, 0);

        if (Input.GetKeyUp(KeyCode.Space) && !isFired)
        {
            Debug.Log("fire!");
            Fire();
        }
    }

    void Fire()
    {
        //isFired = true;
        GameObject shellInstance = Instantiate(shellPrefab,
                                                fireTransform.position,
                                                fireTransform.rotation) as GameObject;

        if (shellInstance)
        {
            shellInstance.tag = "Shell";
            Rigidbody shellRB = shellInstance.GetComponent<Rigidbody>();
            shellRB.velocity = 15.0f * fireTransform.forward;
            Debug.Log("velocity");
        }
    }
}

1 Ответ

0 голосов
/ 16 февраля 2019

Также обычно не рекомендуется устанавливать скорость твердого тела, но вы можете использовать метод Rigidbody.AddForce() для добавления силы к твердому телу.Если вы просто хотите добавить силу в начале, вы можете установить импульсный режим в функции, например: rb.AddForce(Vector3.forward, ForceMode2D.Impulse);

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

public class TankBehaviour : MonoBehaviour
{

    public GameObject shellPrefab;
    public Transform fireTransform;
    private bool isFired = false;

    public float bulletSpeed;

    void Update()
    {
        float x = Input.GetAxis("Horizontal");
        float y = Input.GetAxis("Vertical");

        transform.position += transform.forward * y;
        transform.Rotate(0, x, 0);

        if (Input.GetKeyUp(KeyCode.Space) && !isFired)
        {
            Debug.Log("fire!");
            Fire();
        }
    }

    void Fire()
    {
        //isFired = true;
        GameObject shellInstance = Instantiate(shellPrefab, fireTransform.position, fireTransform.rotation) as GameObject;

        if (shellInstance)
        {
            shellInstance.tag = "Shell";
            Rigidbody shellRB = shellInstance.GetComponent<Rigidbody>();
            shellRB.AddForce(15f * transform.forward, ForceMode.Impulse);
            Debug.Log("velocity");
        }
    }
}

Надеюсь, это поможет!

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