首页 > 解决方案 > 抛出对象的代码没有获得任何高度

问题描述

我正在尝试通过单击鼠标实例化并将对象抛向空中。物体将按预期生成,但在投掷时不会增加任何高度。

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

public class shooter : MonoBehaviour
{
public GameObject powercell; //link to the powerCell prefab
public GameObject Dynamite; // same as above but for dyno
public int no_cell; //number of powerCell owned
public int no_Dynamite; // same as above but for dyno
public AudioClip throwSound; //throw sound
public float throwSpeed = 20;//throw speed


void Start()
{
    no_Dynamite = 0; // no dynos on spawn
    no_cell = 10000; // one cell on spawn
}

public void Update()
{
    //if left control (fire1) pressed, and we still have at least 1 cell
    if (Input.GetButtonDown("Fire1") && no_cell > 0)
    {
        no_cell--; //reduce the cell
                   //play throw sound
        AudioSource.PlayClipAtPoint(throwSound, transform.position);
        //instantaite the power cel as game object
        GameObject cell = Instantiate(powercell, transform.position,
        transform.rotation) as GameObject;
        //ask physics engine to ignore collison between
        //power cell and our FPSControler
        Physics.IgnoreCollision(transform.root.GetComponent<Collider>(),
        cell.GetComponent<Collider>(), true);
        //give the powerCell a velocity so that it moves forward
        cell.GetComponent<Rigidbody>().velocity = transform.forward * throwSpeed;
    }


    if (Input.GetButtonDown("Fire2") && no_Dynamite > 0)
    {
        no_Dynamite--; //reduce the cell
                       //play throw sound
        AudioSource.PlayClipAtPoint(throwSound, transform.position);
        //instantaite the power cel as game object
        GameObject Dyn = Instantiate(Dynamite, transform.position,
        transform.rotation) as GameObject;
        //ask physics engine to ignore collison between
        //power cell and our FPSControler
        Physics.IgnoreCollision(transform.root.GetComponent<Collider>(),
        Dyn.GetComponent<Collider>(), true);
        //give the powerCell a velocity so that it moves forward
        Dyn.GetComponent<Rigidbody>().velocity = transform.forward * throwSpeed;
    }
}

//
void OnTriggerEnter(Collider other)
{
    if (other.gameObject.tag == "Bomb") // give us cells when we pick up the collectable
    {
        no_Dynamite = 3; ; //increment that boi
        Destroy(other.gameObject);
    }

}
}

标签: c#unity3dgame-physics

解决方案


您需要为被抛出物体的速度添加一个向上的分量。0如果你知道你想要在to的范围内向上多少1,你可以用它Vector3.Slerp来找出你需要的方向。

// give the dynamite a velocity so that it moves up + forward
Rigidbody dynamiteRB = Dyn.GetComponent<Rigidbody>();

float upness = 0.5f; // 0f = horizontal ~ 1f = vertical
Vector3 throwDirection = Vector3.Slerp(
        transform.forward, 
        Vector3.up, 
        upness
        );

dynamiteRB.velocity = throwDirection * throwSpeed;

如果您想根据相机进行投掷,但想根据相机给您的角度调整角度,您可以将方向设置为Camera.main.transform

// give the dynamite a velocity so that it moves up + forward
Rigidbody dynamiteRB = Dyn.GetComponent<Rigidbody>();

// 0f   = aim exact direction the camera is pointing
// 0.2f = aim slightly higher than camera is pointing
// 1f   = aim directly up
float additionalUpness = 0.0f; 
Vector3 throwDirection = Vector3.Slerp(
        Camera.main.transform.forward, 
        Vector3.up, 
        additionalUpness 
        );

dynamiteRB.velocity = throwDirection * throwSpeed;

推荐阅读