Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

AddForce in the direction of a local axis (C#)

Discussion in 'Physics' started by MG_Laird, May 14, 2015.

  1. MG_Laird

    MG_Laird

    Joined:
    Apr 18, 2015
    Posts:
    4
    Hello there everyone... and welcome to my first Unity forum question!!! (Epic drum roll?)


    SOME BACKGROUND INFO!

    I am in the process of creating my first game! Since this is my first game, and also the first time that I have really done any actual scripting, I am learning as I go. If push were to come to shove on the topic you could probably designate me as an intermediate-ish programmer... decent knowledge of the basics (and somewhat complex ideas) about C# scripting, but learning about the more advanced topics as I progress, and a lot of the rather difficult stuff still outside of my grasp. Why do you need to know this?? Well... I don't really know, but I figured it may help with your answer, so I added it in!

    I am making a 2.5D physics based side scrolling platformer for the Android platform.
    I am using C#.... yes it was in the title, but I feel the need to reiterate it.
    I am horribly confused.

    THE BASIC IDEA OF WHAT I AM TRYING TO DO!

    I am at the point in my design where I need to create what I like to call a "variable boost cube".

    What I need to basically do is have a cube in my scene (as either a cube with an OnCollisionEnter script, or as a trigger with OnTriggerEnter)... and have that cube apply force to any other object that collides with it. The cube must also ONLY apply force in the direction of its local X axis (or any other axis I suppose... I just chose X for simplicity sake).

    The idea behind this is I want to basically be able to rotate the cube in the editor, and when something hits it during runtime, whatever direction that X axis is facing, is the direction that the colliding object will be pushed.

    EDIT: I suppose I should add that this cube will remain stationary in the level. Once I place and rotate it to my desired specifications, it will stay there permanently (or until I decide to change the position during design).

    I have tried searching around for an answer and can't quite seem to find anything that lets me wrap my head around this issue. Any help you can give would be greatly appreciated, and if you need more clarification please do let me know! Thank you very much!

    Below is an image I created for a visual illustration of what I am trying to accomplish:
     

    Attached Files:

  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,199
    You want to add force to stuff in the direction of your cube's positive x-axis?

    That's transform.right:

    Code (CSharp):
    1. public void OnCollisionEnter(Collision collision) {
    2.     if(collision.gameObject.rigidbody)
    3.         collision.gameObject.rigidbody.AddForce(transform.right * 20f, ForceMode.VelocityChange);
    4. }
    VelocityChange makes the thing fly quite gloriously. If you're looking for that arcady feel, that's the way to go.
     
    rbosse and MG_Laird like this.
  3. MG_Laird

    MG_Laird

    Joined:
    Apr 18, 2015
    Posts:
    4

    Fantastic! Thank you very much! This works perfectly!