我不明白如何在我的 Unity 版本中实现霰弹枪跳跃:https://www.youtube.com/watch?v=FUkdz8jYt3w
这是如何工作的?
我正在使用看起来像胶囊的“第一人称 Controller ” 有图像:/image/tIkjz.jpg
这适用于旧版本的 Unity 4.5.5。我尝试过 RigidBody,但什么也没发生。我尝试过Transform
,但还是没有结果。
第一人称 Controller :
using UnityEngine;
using System.Collections;
public class run : MonoBehaviour
{
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
public Transform character;
public int CharacterForce = 5000;
public int time = 1;
void Update()
{
CharacterController controller =
GetComponent<CharacterController>();
if (controller.isGrounded)
{
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis ("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move (moveDirection * Time.deltaTime);
if (Input.GetMouseButtonDown(0))
{
Transform BulletInstance = (Transform)Instantiate(character, GameObject.Find("CameraRSP").transform.position, Quaternion.identity);
BulletInstance.GetComponent<Rigidbody>().AddForce(transform.forward * CharacterForce);
}
}
}
“手榴弹”代码:
using UnityEngine;
using System.Collections;
public class grenade : MonoBehaviour
{
public Transform GrenadeF;
public int force = 500;
public float radius;
void Start()
{
Collider[] col = Physics.OverlapSphere(transform.position, radius);
foreach (Collider c in col)
{
c.GetComponent<Rigidbody>().AddExplosionForce(force, transform.position, radius);
}
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.blue;
Gizmos.DrawWireSphere(transform.position, radius);
}
}
当我使用Transform
时,我预计这会在任何方向上起作用,但它只在一个方向上起作用。
当我使用 RigidBody 时,我预计这会起作用,但我的胶囊甚至没有移动。
最佳答案
查看可选的upwardsModifier
AddExplosionForce(float explosionForce, Vector3 explosionPosition, float explosionRadius, float upwardsModifier = 0.0f, ForceMode mode = ForceMode.Force));
(Unity 4 的 API 不再可用,但我想应该是一样的)
Adjustment to the apparent position of the explosion to make it seem to lift objects.
和
Using this parameter, you can make the explosion appear to throw objects up into the air, which can give a more dramatic effect rather than a simple outward force. Force can be applied only to an active rigidbody.
默认情况下它是0
,所以如果你不通过它,就不会有任何向上的力。
正如您在 API 的示例中看到的那样
Vector3 explosionPos = transform.position;
Collider[] colliders = Physics.OverlapSphere(explosionPos, radius);
foreach (Collider hit in colliders)
{
Rigidbody rb = hit.GetComponent<Rigidbody>();
// |
// v
if (rb) rb.AddExplosionForce(power, explosionPos, radius, 3.0f);
}
他们通过了,例如3.0f
作为 upwardsModifier
。这使得爆炸
appear to be centred 3.0 units below its actual position for purposes of calculating the force direction (ie, the centre and the radius of effect are not modified).
注意:在智能手机上输入,因此没有保修,但我希望这个想法能够清晰
关于c# - 我怎样才能重复这个提升(推送)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56225898/