我想创建像 'chilly snow' 游戏中那样的球运动。我想弯曲球,但它在旋转

I want to create the ball movement like the one in 'chilly snow' game. I want to curve the ball, but it revolves

我是unity新手,很多东西都不懂。我一直在看教程,我看到一个人在其中创建了著名 'Chilly Snow' 的复制品。游戏已经完成,但球的运动不像在寒冷的雪地里那样。当我按下鼠标按钮时,球开始连续旋转。我想知道如何创造那种运动,让球在曲线上左右转动,但不会进入轨道。我用谷歌搜索了很多,但没能找到我需要的结果。如果有人能指出我正确的方向,我将不胜感激。图片已附上。Chilly Snow | Movement of my ball

public class movement : MonoBehaviour {

private float points;
public float playerSpeed;
private float rotationSpeed;
public Text score;
private bool isMovingLeft;
public GameObject player;
public bool isDead;




void Start () {

    Time.timeScale = 0;
    isDead = false;
    isMovingLeft = true;
    points = 0;



}


void Update () 
{
    if (isDead == false) 
    {
        points += Time.deltaTime;
    }

    transform.Translate (Vector3.down * playerSpeed * Time.deltaTime);

    if (Input.GetMouseButtonDown (0)) 
    {
        Time.timeScale = 1;
        isMovingLeft = !isMovingLeft;
        rotationSpeed += 0.5f * Time.deltaTime;

    }

    if (Input.GetMouseButton (0)) 
    {
        rotationSpeed = 1f;
    }

    if (isMovingLeft) {
        rotationSpeed += 1.5f * Time.deltaTime;
        transform.Rotate(0,0,rotationSpeed);

    } else
        transform.Rotate(0,0, - rotationSpeed);
}


void OnTriggerEnter2D(Collider2D other)
{
    if (other.tag == "Obstacle") {
        Die ();
    }
}

public void Die()
{


    playerSpeed = 0f;
    isDead = true;
    Invoke ("Restart", 2f);

}

void Restart(){

    SceneManager.LoadScene ("Ski_scene_1");

}

void FixedUpdate()
{
    score.GetComponent<Text>().text = points.ToString("0");
}

}

您需要诸如正弦运动之类的东西或您喜欢的任何其他图表。

一个例子是这样的;

gameObject.transform.Translate(Vector3.right * Time.deltaTime*cubeSpeed);

gameObject.transform.position += transform.up * Mathf.Sin (Time.fixedTime * 3.0f  ) * 0.1f;

以上伪是二维图形模拟,可以根据自己的情况进行调整。

物体总是在做正弦运动的同时向右上下移动。因为上下速度不固定,所以你得到 正弦曲线 正弦曲线 运动。

在你的例子中,当物体总是向下移动时,它会正弦曲线向左和向右移动。

你的运动是基于旋转的所以,如果你给这个正弦速度作为你的旋转速度,你可以实现这个。


另一种方法可以是 lerpslerp

Lerp 允许您在 2 个向量之间进行比较平滑的交易。

就像在 X 秒内从 A 点移动到 B 点。

轮换需要 Quaternion.Lerp Unity Answers 上有一个很好的答案,你可以检查一下,如果你以前没有。


希望这对您有所帮助!干杯!

这是我如何在不进行旋转的情况下处理它...使用您的代码。

public class movement : MonoBehaviour {

private float points;
public Text score;
public GameObject player;
public bool isDead;

private float currentXSpeed;
private float targetSpeed;
public float maxXSpeed;
public float speedChange;

void Start () {
    Time.timeScale = 0;
    isDead = false;
    isMovingLeft = true;
    points = 0;
    targetSpeed = maxXSpeed;
}


void Update () 
{
    if (isDead == false) 
    {
        points += Time.deltaTime;
    }

    if(Input.GetMouseButtonDown(0))
    {
         Time.timeScale = 1;
         targetSpeed = -targetSpeed;
    }

    currentSpeed = mathf.MoveTowards(currentSpeed, targetSpeed, speedChange * Time.deltaTime);
    Vector3 movementDirection = new Vector3(currentSpeed, Vector3.down.y * playerSpeed, 0.0f);
    transform.Translate (movementDirection * Time.deltaTime);
}

void OnTriggerEnter2D(Collider2D other)
{
    if (other.tag == "Obstacle") {
        Die ();
    }
}

public void Die()
{
    playerSpeed = 0f;
    isDead = true;
    Invoke ("Restart", 2f);
}

void Restart(){
    SceneManager.LoadScene ("Ski_scene_1");
}

void FixedUpdate()
{
    score.GetComponent<Text>().text = points.ToString("0");
}

}