- Home /
Question by
Arsarys · Jan 26, 2019 at 04:23 AM ·
rigidbody.addforcecontinuousforcemode
Rigidbody2d.addforce with forcemode2d.impulse not working with Vertical
I try to make my character move four direction on the map and i want to stop it when there is no input. Script works on horizontal moves but not working with vertical moves. It is adds forces continuous and it is look like force stacking. I need your help :)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float speed;
public int boost;
private Rigidbody2D rb2d;
void Start()
{
rb2d = GetComponent<Rigidbody2D>();
speed = 2f;
boost = 10;
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal") * boost ;
float moveVertical = Input.GetAxis("Vertical");
Vector2 movement = new Vector2(moveHorizontal, moveVertical);
rb2d.AddForce(movement * speed, ForceMode2D.Impulse);
}
}
Comment
Best Answer
Answer by sean244 · Jan 26, 2019 at 04:57 AM
Replace
rb2d.AddForce(movement * speed, ForceMode2D.Impulse);
with
rb2d.velocity = movement * speed;
Your answer
Follow this Question
Related Questions
ForceMode.VelocityChange vs. simply adding to .velocity 0 Answers
Continuously Rotating Script? 2 Answers
How to make a continuous runner on a planet with gravity? 1 Answer
transform.forward problem 2 Answers