抛出对象的代码不会增加任何高度
Code to throw object doesn't gain any height
我正在尝试通过单击鼠标来实例化对象并将其抛向空中。对象将按预期生成,但不会在投掷时获得任何高度。
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);
}
}
}
您需要为抛出物体的速度添加一个向上分量。如果您知道在 0
到 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;
我正在尝试通过单击鼠标来实例化对象并将其抛向空中。对象将按预期生成,但不会在投掷时获得任何高度。
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);
}
}
}
您需要为抛出物体的速度添加一个向上分量。如果您知道在 0
到 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;