所以有我的角色的 Rigidbody2D 附件的代码,但是当设置为 Kinematic 时他不会移动(仅适用于 Dynamic),但我想要 Kinematic,因为他与动态对象发生碰撞,我不想让他向左移动一触即发。

UI:我是初学者,我只想为 Android 制作我的第一个游戏,同时也为我的英语感到抱歉。 :D

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Movement : MonoBehaviour
{
    //variables
    public float moveSpeed = 300;
    public GameObject character;

    private Rigidbody2D characterBody;
    private float ScreenWidth;


    // Use this for initialization
    void Start()
    {
        ScreenWidth = Screen.width;
        characterBody = character.GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        int i = 0;
        //loop over every touch found
        while (i < Input.touchCount)
        {
            if (Input.GetTouch(i).position.x > ScreenWidth / 2)
            {
                //move right
                RunCharacter(1.0f);
            }
            if (Input.GetTouch(i).position.x < ScreenWidth / 2)
            {
                //move left
                RunCharacter(-1.0f);
            }
            ++i;
        }
    }
    void FixedUpdate()
    {
#if UNITY_EDITOR
        RunCharacter(Input.GetAxis("Horizontal"));
#endif
    }

    private void RunCharacter(float horizontalInput)
    {
        //move player
        characterBody.AddForce(new Vector2(horizontalInput * moveSpeed * Time.deltaTime, 0));

    }
}

最佳答案

Unity docs



因此,与其施加力,不如改变它的位置。像这样的东西:

private void RunCharacter(float horizontalInput)
{
    //move player
    characterBody.transform.position += new Vector2(horizontalInput * moveSpeed * Time.deltaTime, 0);

}

关于c# - 我怎样才能继续触摸 "Kinematic"Rigidbody2D?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61120972/

10-10 07:08