简体   繁体   English

为什么播放器运动出错-Unity 3D C#

[英]Why Error in player movement - Unity 3D C#

I have a ball in my scene that I want to move it forward in a constantly speed, so I made this code for the ball: 我的场景中有一个球,希望以恒定的速度向前移动它,因此我为球编写了以下代码:

public float speed = 100f;

    void FixedUpdate () {

        //rb.AddRelativeForce(Vector3.forward * speed * Time.deltaTime);
        transform.position += Vector3.forward * Time.deltaTime * speed;
        //rb.AddForce(0, 0, speed *Time.deltaTime);
        //rb.velocity = transform.forward * speed;
    }

NB: I've tried all the codes you see in the comments (//...) 注意:我已经尝试了您在注释中看到的所有代码(// ...)

The problem is sometimes the ball is destroying by itself, and sometimes the speed decrease, or when the ball collide with other object it go forward and backward in a weird movement! 问题是球有时会自身破坏,有时速度会降低,或者当球与其他物体碰撞时,它会以怪异的动作前进和后退!

Here's the entire code: 这是完整的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(AudioSource))]
public class PlayerMovement : MonoBehaviour {

    public AudioSource audioSource1;
    public AudioSource audioSource2;
    public Transform BallDestroyEffect;
    public ParticleSystem BallCollectEffect;
    public ParticleSystem SpeedEffect;
    public ParticleSystem FireWorksEffect;
    public GameObject FloatingTextPrefab;
    public GameObject ContinueButton;

    public Rigidbody rb;
    public float speed = 100f;
    private int count;
    public Text countText;
    public Text winText;
    bool gameStart;

    private void Start()
    {
        BallCollectEffect.Stop();
        FireWorksEffect.Stop();
        ContinueButton.SetActive(false);
        rb = GetComponent<Rigidbody>();

        count = 0;
        setcountText();
        winText.text = "";
        audioSource1 = GetComponent<AudioSource>();
        audioSource2 = GetComponent<AudioSource>();
    }

    void FixedUpdate () {

        //rb.AddRelativeForce(Vector3.forward * speed * Time.deltaTime);
        //transform.position += Vector3.forward * Time.deltaTime * speed;
        //rb.AddForce(0, 0, speed *Time.deltaTime);
        //rb.velocity = transform.forward * speed;
        rb.MovePosition(transform.position + transform.forward * speed);
    }

    void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.CompareTag ("Enemy"))
        {
            audioSource2.Play();
            SpeedEffect.Stop();
            rb.gameObject.SetActive(false);
            Instantiate(BallDestroyEffect, transform.position, transform.rotation);
            FindObjectOfType<GameManager>().EndGame();

        }
        if (collision.gameObject.CompareTag("End"))
        {
            ContinueButton.SetActive(true);
            winText.text = "Level Completed!";
            SpeedEffect.Stop();
            FireWorksEffect.Play();
        }
        if(collision.gameObject.CompareTag("Glass"))
        {
            audioSource1.Play();
            count = count + 3;
            setcountText();
            BallCollectEffect.Play();
            if (FloatingTextPrefab)
            {
                ShowFloatingText();
            }
        }
    }

    void ShowFloatingText()
    {
        Instantiate(FloatingTextPrefab, transform.position, Quaternion.identity, transform);
    }


    void setcountText ()  {
        countText.text = count.ToString();
    }
}

Do you have any suggestions about that? 您对此有何建议?

Rigidbody.MovePosition()

This should be used if you want to continuously move a rigidbody in each FixedUpdate. 如果要在每个FixedUpdate中连续移动刚体 ,则应使用此方法。

Set Rigidbody.position instead, if you want to teleport a rigidbody from one position to another, with no intermediate positions being rendered. 如果要将刚体从一个位置传送到另一个位置,而不渲染任何中间位置, 设置Rigidbody.position

Note the difference in those two sentences. 注意这两个句子的区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM