나는 플랫폼에 서있는 공을 가지고 있으며 나는 스 와이프의 힘에 따라 공이 한 플랫폼에서 다른 플랫폼으로 스 와이프 할 때마다 코드가 작성되도록 코드를 작성했습니다. 순간에 내 플랫폼은 나 자신에 의해 위치에 배치하고 난 그들의 임의의 생성을위한 스크립트를 가지고 있지 않습니다. 내가 가지고있는 유일한 스크립트는 플레이어에서 스 와이프 및 앞으로 이동하는 것입니다.결합력 추가 기능을 사용하여 점프 동작을 스 와이프하는 법.
현재이 동작은 두 방향으로 힘을 가하여 앞뒤로 움직여 발사체 동작을 만듭니다. 그것의 일은 그것의 가정 된 것처럼 보이지만 운동은 너무 느립니다. 나는 그것을 더 빨리 움직이기를 원한다. Iwe는 공의 질량뿐만 아니라 힘으로 노는 것을 시도했다. 그들은 변화를 가져 오지만 여전히 공이 더 빨리 움직이기를 원합니다.
강제하는 것이 가장 좋은 방법입니까? 아니면 다른 방법을 권하고 싶습니까?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SwipeScript : MonoBehaviour {
public float maxTime;
public float minSwipeDist;
float startTime;
float endTime;
Vector3 startPos;
Vector3 endPos;
float swipeDistance;
float swipeTime;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
startTime = Time.time;
startPos = touch.position;
}
else if (touch.phase == TouchPhase.Ended)
{
endTime = Time.time;
endPos = touch.position;
swipeDistance = (endPos - startPos).magnitude;
swipeTime = endTime - startTime;
if (swipeTime < maxTime && swipeDistance > minSwipeDist)
{
swipe();
}
}
}
}
public void swipe()
{
Vector2 distance = endPos - startPos;
if (Mathf.Abs(distance.y) > Mathf.Abs(distance.x))
{
Debug.Log("Swipe up detected");
jump();
}
}
private void jump()
{
Vector2 distance = endPos - startPos;
GetComponent<Rigidbody>().AddForce(new Vector3(0, Mathf.Abs(distance.y/5), Mathf.Abs(distance.y/5)));
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name == "Cube (1)") {
Debug.Log("collision!");
GetComponent<Rigidbody>().velocity = Vector3.zero;
GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
}
}
}
Rigidbody를'Start()'에 저장하면 사용할 때마다 GetComponent를 사용할 필요가 없으므로 Rigidbody를 매우 최적화되지 않은 상태로 저장하십시오. 또한 코드를보다 깨끗하게 만듭니다. '리지드 바디 rb; Start() {rb = GetComponent (); }'그런 다음 나머지 스크립트에서 GetComponent 대신 rb를 사용하십시오. 이론적으로 시간 눈금을 늘려 ('Time.timeScale = 2') 모든 것을 두 배 속도로 만들 수 있습니다. 그러나 나는 그것을 추천하지 않는다. 공이 빨리 움직이기를 원한다면, 더 많은 힘을 더하고 끌기를 증가 시키십시오. https://docs.unity3d.com/ScriptReference/Rigidbody-drag.html – Maakep
문제가 해결 된 경우 적합한 것으로 받아들이십시오. _ 대답을 수락하면 미래의 방문자가이 페이지를 방문하는 데 도움이됩니다. _ – Kardux