抛出对象的代码不会增加高度

时间:2018-11-02 19:52:24

标签: c# unity3d game-physics

我正在尝试实例化对象,并通过单击鼠标将对象扔向空中。这些对象将按预期生成,但不会在抛出时获得任何高度。

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);
    }

}
}

1 个答案:

答案 0 :(得分:2)

您需要向被投掷物体的速度添加一个向上分量。如果您知道要在01的范围内向上移动多少,可以使用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;