简体   繁体   English

在Unity中向一致的方向推动角色

[英]pushing character in a direction consistently in Unity

I have a particle effect that shoots bubbles, and when the character enters the stream of bubbles I'd like to create the effect that the bubbles consistently move the character in the direction they're going. 我有一个粒子效果,可以发射气泡,当角色进入气泡流时,我想创造一种效果,使气泡始终朝着角色前进的方向移动角色。 How would I go about doing that? 我将如何去做? I put a collider in the particle effects already and have tried this code so far. 我已经在粒子效果中添加了对撞机,并且到目前为止已经尝试了此代码。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GeyserPush : MonoBehaviour {

    public GameObject Player;
    public float Speed;



    private void OnTriggerEnter(Collider other)
    {
        if(other.gameObject == Player)
        {
            //other.transform.Translate((Vector3.up * Time.deltaTime), Space.World);
            Player.transform.Translate(Vector3.up * Time.deltaTime * Speed);
            print("Pushing");
        }
    }


    private void OnTriggerExit(Collider other)
    {
        if (other.gameObject == Player)
        {

        }
    }
}

The code is incomplete since I do not have a full solution yet. 由于我还没有完整的解决方案,因此代码不完整。 The character does have a rigidbody equipped to it too. 角色也确实配备有刚体。

Based on the table at the bottom of this link , if both the particles and the player have Rigidbody , it might be better to use OnCollisionEnter() instead. 根据此链接底部的表,如果粒子和播放器都具有Rigidbody ,则最好改用OnCollisionEnter()

You could add a force on the player's Rigidbody per collision in the direction that your particles are moving like so: 每次碰撞时,您可以在粒子的移动方向上向玩家的Rigidbody施加力,如下所示:

private void OnCollisionEnter(Collider other)
{
    if(other.gameObject == Player)
    {
        //other.transform.Translate((Vector3.up * Time.deltaTime), Space.World);
        Player.GetComponent<Rigidbody>().AddForce(targetDirection * Speed);
        print("Pushing");
    }
}

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

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