[英]I am trying to make an FPS soccer game using Unity, but my script isn't working
So, I am trying to create a soccer game from scratch... all I have done until now, is setting up the ball.所以,我正在尝试从头开始创建一个足球游戏......到目前为止我所做的只是设置球。 This is how I want it to work: When the player collides with the ball, the ball jumps forward a bit.
这就是我希望它的工作方式:当玩家与球发生碰撞时,球会向前跳一点。 If you start running the ball will be pushed further away.
如果您开始奔跑,球将被推得更远。
Now, here is my script for the ball (I am using the standard FPSController as character):现在,这是我的球脚本(我使用标准 FPSController 作为角色):
using UnityEngine;
using System.Collections;
public class BallController : MonoBehaviour {
private Rigidbody rb;
public GameObject character;
public float moveSpeed = 1000;
public float shootSpeed = 2000;
bool isTurnedUp = false;
bool isTurnedDown = false;
bool done = false;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate () {
//Debug.Log(isTurnedUp + ", " + isTurnedDown);
switch (character.GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController>().m_IsWalking)
{
case true:
if (isTurnedUp == false)
{
moveSpeed = moveSpeed / 1.4f;
isTurnedUp = true;
isTurnedDown = false;
}
break;
case false:
if (isTurnedDown == false)
{
moveSpeed = moveSpeed * 1.4f;
isTurnedDown = true;
isTurnedUp = false;
}
break;
}
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (Vector3.Distance(gameObject.transform.position, character.transform.position) <= 5)
{
float distance = Vector3.Distance(gameObject.transform.position, character.transform.position);
}
}
}
void OnCollisionEnter(Collision collision) {
FixedUpdate();
if (done == false) {
rb.AddForce(Vector3.forward * moveSpeed, ForceMode.Impulse);
done = true;
}
else {
done = false;
}
}
//other
void OnDrawGizmosSelected()
{
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(transform.position, 2);
}
}
My problem is that the ball doesn't behave how I want it... it feels like it's about luck if the ball will jump forward when I touch it.我的问题是球没有按照我想要的方式运行……如果我触摸它时球会向前跳,感觉就像是运气。 Can someone tell me what I did wrong?
有人能告诉我我做错了什么吗?
Inside of OnCollisionEnter
you need to ensure the ball can only be kicked by the player.在
OnCollisionEnter
内部,您需要确保球只能由玩家踢。 You can check whether or not the player has collided with the ball by checking the name or tag of the collision.您可以通过检查碰撞的名称或标签来检查玩家是否与球发生碰撞。 The following example uses the name and assumes your player GameObject is named "Player".
以下示例使用该名称并假设您的玩家 GameObject 名为“Player”。
Remove the done
flag since this will only allow the player to kick the ball every other time they collide, and remove the FixedUpdate()
call since FixedUpdate()
is already called automatically every physics calculation.删除
done
标志,因为这将只允许玩家在每次碰撞时踢球,并删除FixedUpdate()
调用,因为FixedUpdate()
已经在每次物理计算时自动调用。
Finally, if you want to kick the ball away from the player, then you need to calculate the direction away from the collision point instead of using Vector3.forward
as seen below.最后,如果您想将球踢离玩家,那么您需要计算远离碰撞点的方向,而不是使用
Vector3.forward
,如下所示。
void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.name == "Player")
{
Vector3 direction = (collision.transform.position - transform.position).normalized;
rb.AddForce(-direction * moveSpeed, ForceMode.Impulse);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.