Home > Net >  how to make jump and step forward in unity 3d?
how to make jump and step forward in unity 3d?

Time:05-30

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.

CodePudding user response:

1.- "but did it in a jump".
You can check the Transform.Translate documentation. 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."
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.
I would try:
45º to the right: Vector3(1, 0, 1)
45º to the left: Vector3(-1, 0, 1)
40º to the right: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)))

For a distance d in the desired direction, include that in your point calculation: distance d 40º to the right:
d * Vector3(Mathf.sin(Mathf.Deg2Rad(40)), 0, Mathf.cos(Mathf.Deg2Rad(40)))
distance d 40º to the left:
d * Vector3(-d * Mathf.sin(Mathf.Deg2Rad(40)), 0, Mathf.cos(Mathf.Deg2Rad(40)))

  • Related