gpt4 book ai didi

c# - 我怎样才能在触摸 "Kinematic"Rigidbody2D上移动?

转载 作者:行者123 更新时间:2023-12-02 09:40:29 25 4
gpt4 key购买 nike

所以有我的角色的 Rigidbody2D 附件的代码,但当设置为运动学(仅适用于动态)时他不会移动,但我想要运动学,因为他与动态对象发生碰撞,我不希望他移动只需向左和向右触摸即可。

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 ,

If isKinematic is enabled, Forces, collisions or joints will not affect the rigidbody anymore.

所以不用施加力,只需改变它的位置即可。像这样的事情:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com