简体   繁体   English

我如何在玩家面对的方向上增加力量

[英]how do i add force in the direction the player is facing

So the code is on a object that makes the player bounce and it gets the rb and adds a force that makes it go up but I would like it also to give the player a boost in the direction their facing.所以代码在 object 上,它使玩家弹跳并获得 rb 并增加一个力,使其 go 向上,但我也希望它能够让玩家在他们面对的方向上得到提升。

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

public class bounce : MonoBehaviour
{
    [Range(100, 10000)]
    public float bouncehight;

    [Range(100, 10000)]
    public float boost;

    private void OnCollisionEnter(Collision collision)
    {
        GameObject bouncer = collision.gameObject;
        Rigidbody rb = bouncer.GetComponent<Rigidbody>();

        rb.AddForce(Vector3.up * bouncehight);

        //so here i would call or something 

        rb.AddForce(Vector3.forward * boost *look);


    }

You can get the direction vector of a GameObject viaTransform.forward .您可以通过GameObject获得游戏Transform.forward的方向向量。

This would probably be something like rb.AddForce(transform.forward * boost)这可能类似于rb.AddForce(transform.forward * boost)

Easiest way would be to use transform.forward:最简单的方法是使用 transform.forward:

rb.AddForce(transform.forward * forceAmount)

Alternatively you can count it yourself for the same result:或者,您可以自己计算相同的结果:

rb.AddForce(Vector3.forward * transform.rotation * forceAmount)

Edit: since you are inside bounce class transform.forward refers to the bounce, hence always the same direction.编辑:因为你在反弹 class transform.forward 是指反弹,因此总是相同的方向。 Use rb.gameObject:使用 rb.gameObject:

rb.AddForce(Vector3.forward * rb.gameObject.transform.rotation * forceAmount)

Or或者

  rb.AddForce(rb.gameObject.transform.forward * forceAmount)

to refer to the player's rotation instead指代球员的轮换

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

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