I want to apply a force to a character when the player applies a switch.
gameObject.rigidbody.AddForce((3,1,0)*10));
when applying force with the above statement, my character is moving 3 units in x direction. After it completed it’s move in x direction, it goes 1 in y direction. Isn’t it supposed to go in (3,1,0) direction? When I’m using the same statement to player, it goes the right way that I wanted. But it isn’t applying properly to the character. Am I missing something here?
Ok as far as i can see, you’re trying to implement this in other gameobject instead of player right? if this is the case, then you’re doing it wrong, you need to send the action to te player gameobject, something like this:
void YourSwitchMetod()
{
//First get the player from the world into a variable named player. Make sure the tag of the player GO matches
GameObject player = GameObject.FindGameObjectWithTag("Player");
//then apply the force to the player if is not null
if(player)
{
//Now apply the force to the rigidbody of the player
player.rigidbody.AddRelativeForce(new Vector3(3,1,0),ForceMode.Impulse);
}
}
if this isn’t your case then just make sure you’re using the AddForce function correctly like this:
//just apply the force to the body and make sure you're doing from the FixedUpdate()
void FixedUpdate()
{
rigidbody.AddForce(new Vector3(3,1,0),ForceMode.Impulse);
}
if you want to make just one “shoot” function you can do it like this: