简体   繁体   English

我正在尝试使用 Unity 制作 FPS 足球游戏,但我的脚本不起作用

[英]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.

相关问题 我目前正在开发一款游戏,但我正在尝试做一个 Else 声明,但它不起作用 - I'm currently working on a game however I am trying to make an Else statement but it isn't working 我正在努力让我的 Unity 游戏在游戏结束后重新启动 - I am trying to make it so that my Unity game restarts after the game has ended 我正在尝试在我的数据集中实现,但无法正常工作 - I am trying to implement in my Dataset LIKE but it isn't working 我正在尝试为我的 Unity 游戏编写一个重置按钮 - I am trying to code a reset button for my Unity game 我正在尝试统一构建游戏,但这给了我一个错误 - I am trying to build my game in unity but it is giving me an error 我正在尝试制作一个损坏脚本,但它出现了一个错误,指出名称空间“Player”无法识别 - I am trying to make a script for damage but it comes up with an error saying the namespace “Player” isn't recognized 我正在尝试使用 unity 制作游戏,但我的 Jump 功能不起作用 - I'm try to make game using unity and my Jump functionality not working 我正在用统一引擎制作 2d 平台游戏,但我无法让我的玩家跳跃 - I am making a 2d platformer game in unity engine, but I am unable to make my player jump 我正在尝试将Unity游戏连接到MongoDB,但是使用异步方法和任务时出现错误,我该如何解决? - I am I am trying to connect Unity game to MongoDB but I have an error using an a async method and task, How can I solve that? 我正在使用统一制作我的第一个 2d 游戏(我是菜鸟),我试图制作一个死亡屏障,将角色传送回开始 - I am using unity to make my first 2d game (im a noob) and i tried to make a death barrier that will teleport the charecter back to the start
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM