Я пытаюсь использовать код .AddForce вместе с кодом .movePosition, но .movePosition, заставляющий .AddForce, ничего не делать. Я думаю, это потому, что строка: «Vector2 motion = new Vector2 (moveHorizontal, 0);» но я действительно не знаю, как это исправить. Код .AddForce работает так, как задумано самим собой.
Изменить: опубликовал полный код.
using UnityEngine; using System.Collections;
открытый класс CompletePlayerController: MonoBehaviour {
public float speed; //Floating point variable to store the player's movement speed.
public float jumpforce;
private Rigidbody2D rb2d; //Store a reference to the Rigidbody2D component required to use 2D Physics.
// Use this for initialization
void Start()
{
//Get and store a reference to the Rigidbody2D component so that we can access it.
rb2d = GetComponent<Rigidbody2D> ();
}
//FixedUpdate is called at a fixed interval and is independent of frame rate. Put physics code here.
void FixedUpdate()
{
Jump();
//Store the current horizontal input in the float moveHorizontal.
float moveHorizontal = Input.GetAxis ("Horizontal");
//Store the current vertical input in the float moveVertical.
//Use the two store floats to create a new Vector2 variable movement.
Vector2 movement = new Vector2 (moveHorizontal, 0);
//Call the AddForce function of our Rigidbody2D rb2d supplying movement multiplied by speed to move our player.
rb2d.MovePosition ((Vector2)transform.position + (movement * speed * Time.deltaTime));
}
void Jump(){
if (Input.GetButtonDown("Vertical")){
rb2d.AddForce(new Vector2(0, jumpforce), ForceMode2D.Impulse);
}
}
}
AddForce
код. AddForce применит силу к объекту, а MovePosition будет перемещать объект с помощью интерполяции. Похоже, что MovePosition просто перезапишет приложенную силу - person Derek C.   schedule 27.01.2020