【问题标题】:How can i make to move on touch "Kinematic" Rigidbody2D?我怎样才能继续触摸“Kinematic”Rigidbody2D?
【发布时间】:2020-07-22 01:29:25
【问题描述】:

所以有我的角色的 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));

    }
}

【问题讨论】:

    标签: c# android unity3d


    【解决方案1】:

    来自Unity docs

    如果启用 isKinematic,力、碰撞或关节将不再影响刚体。

    所以不要施加力,只需改变它的位置。像这样的:

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

    【讨论】:

      猜你喜欢
      • 2012-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-21
      • 2012-01-12
      • 1970-01-01
      • 2022-07-25
      相关资源
      最近更新 更多