简体   繁体   English

如何在unity 3d中跳跃前进?

[英]how to make jump and step forward in unity 3d?

The player must press the buttons and the character move one step forward, but did it in a jump.玩家必须按下按钮,角色才能向前移动一步,但是是跳跃式的。

using System.Collections.Generic;
using UnityEngine;

public class playerrr : MonoBehaviour
{
    public void MoveUp()
    {
        transform.Translate(0f, 0f, 1f);
    }
    public void MoveLeft()
    {
        transform.Translate(-1f, 0f, 0f);
    }
    public void MoveRight()
    {
        transform.Translate(1f, 0f, 0f);
    }
}

and the movement to the left and right must be done not by 90 degrees, but by about 40.并且向左和向右的移动不能做90度,而是大约40度。

1.- "but did it in a jump". 1.-“但是一跃而起”。
You can check the Transform.Translate documentation .您可以查看Transform.Translate 文档 The transformations takes place at the "sudden" moment where the instruction is executed, meaning that the movement is applied "at once".转换发生在执行指令的“突然”时刻,这意味着“立即”应用了运动。 If you want it to be gradual, there is an example in the documentation itself that does just that:如果您希望它是渐进的,文档本身中有一个示例可以做到这一点:

public class ExampleClass : MonoBehaviour
{
    void Update()
    {
        // Move the object forward along its z axis 1 unit/second.
        transform.Translate(Vector3.forward * Time.deltaTime);

        // Move the object upward in world space 1 unit/second.
        transform.Translate(Vector3.up * Time.deltaTime, Space.World);
    }
}

2.- "not by 90 degrees, but by about 40." 2.-“不是90度,而是大约40度。”
Then you need to calculate that direction and move there.然后你需要计算那个方向并移动到那里。 Notice that you can choose the " Space.Self" in the arguments to define the translation taking into account the local axis system of the gameobject of interest (usually aligned with this ) you then choose a point of your choice that meets your requirements for the translation.请注意,您可以选择 arguments 中的“Space.Self”来定义翻译,同时考虑到感兴趣的游戏对象的局部轴系统(通常与对齐),然后您选择一个满足您的要求的点翻译。
I would try:我会尝试:
45º to the right: Vector3(1, 0, 1)向右 45º: Vector3(1, 0, 1)
45º to the left: Vector3(-1, 0, 1)向左 45º: Vector3(-1, 0, 1)
40º to the right: Vector3(Mathf.sin(Mathf.Deg2Rad(40)), 0, Mathf.cos(Mathf.Deg2Rad(40)))向右 40º: Vector3(Mathf.sin(Mathf.Deg2Rad(40)), 0, Mathf.cos(Mathf.Deg2Rad(40)))
40º to the left: Vector3(-Mathf.sin(Mathf.Deg2Rad(40)), 0, Mathf.cos(Mathf.Deg2Rad(40)))向左 40º: Vector3(-Mathf.sin(Mathf.Deg2Rad(40)), 0, Mathf.cos(Mathf.Deg2Rad(40)))

For a distance d in the desired direction, include that in your point calculation: distance d 40º to the right:对于所需方向的距离d ,将其包括在您的点计算中:距离 d 向右 40º:
d * Vector3(Mathf.sin(Mathf.Deg2Rad(40)), 0, Mathf.cos(Mathf.Deg2Rad(40)))
distance d 40º to the left:向左 d 40º 的距离:
d * Vector3(-d * Mathf.sin(Mathf.Deg2Rad(40)), 0, Mathf.cos(Mathf.Deg2Rad(40)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM