I need too make something shoot foward

I’m making a game where the character is shot forward in the direction that it is facing, which includes the Y coordinate, when left click is pressed. I am a beginner so I mostly rely on Unity answers and tutorials, but I can’t find something that applies to what I want to do. the thing doesn’t stop until it hits something. The only code I have is this

function Update() {
    if(Input.GetMouseButtonDown(0))
        Debug.Log(Supposed too move);
}

The whole point of the game is too hit things by flinging yourself around so I have a Rigid body physics thing on it so you can just fly around. It’s Okay if I have too change that.

Hi,

You do need to have a RigidBody component :slight_smile: Then you want to use AddForce. AddForce requires a force through a Vector3, since you want to to pushed forward, you’ll need to use transform.forward as a base to build your force !

You need a couple variables:

RigidBody rb;
float forceAmount;

And initialize them:

void Start () {
   rb = GetComponent<RigidBody>();
   forceAmount = 106;
}

Then you want to write the function that you will call when mouse button 0 is pressed:

void BeNiceAndShootMeForward() {
   rb.AddForce(transform.forward * forceAmount);
   // from here, you can add whatever else you want
   // to do when you're pushed away ...
   // like disable gravity, play an animation, ...
}

Now call it from Update:

void Update () {
   if(Input.GetMouseButtonDown(0))
      BeNiceAndShootMeForward();
}

And since i realize only now that you use JS, here the translation:

rb : RigidBody;
forceAmount : float;

function Start () {
   rb = GetComponent<RigidBody>();
   forceAmount = 106;
}

function Update () {
   if(Input.GetMouseButtonDown(0))
      BeNiceAndShootMeForward();
}

function BeNiceAndShootMeForward() {
   rb.AddForce(transform.forward * forceAmount);
   // from here, you can add whatever else you want
   // to do when you're pushed away ...
   // like disable gravity, play an animation, ...
}