Home > Enterprise >  how do i add force in the direction the player is facing
how do i add force in the direction the player is facing

Time:08-24

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.

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);


    }

CodePudding user response:

You can get the direction vector of a GameObject via Transform.forward.

This would probably be something like rb.AddForce(transform.forward * boost)

CodePudding user response:

Easiest way would be to use transform.forward:

rb.AddForce(transform.forward * forceAmount)

Alternatively you can count it yourself for the same result:

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