简体   繁体   English

Unity3D使用GetAxis变换位置移动

[英]Unity3D transform position movement with GetAxis

I have implemented a click 2 move script and i'm trying to use a method that already gets a Vector3 of where I clicked and moves the player via CharacterController to it. 我已经实现了单击2移动脚本,并且我试图使用一种方法,该方法已经获取了我单击的位置的Vector3,并通过CharacterController将播放器移动到了它。

is there a way to take the transforms current position, get the values from GetAxis while the keys are down and calculate its new position. 有没有办法获取转换的当前位置,在按下键的同时从GetAxis获取值并计算其新位置。 when the vertical/horizontal controls are release the Vector3 would be assigned the transforms current position so it will stop moving 释放垂直/水平控件时,将为Vector3分配变换的当前位置,以使其停止移动

the is what is used to move to the position. 这是用来移动到该位置的东西。

void MoveToPosition()
{
    if (Vector3.Distance(transform.position, position) > 1)
    {
        Quaternion newRotation = Quaternion.LookRotation(position - transform.position);
        newRotation.x = 0f;
        newRotation.z = 0f;
        transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 10);
        controller.SimpleMove(transform.forward * speed);
        animation.CrossFade(run.name);
    }
    else
    {
        animation.CrossFade(idle.name);
    }
}

position is assigned from a raycast so I would like to use the players current position, what ever the calculations are required with getaxis and adjust accordingly 位置是从光线投射分配的,所以我想使用玩家当前的位置,使用getaxis进行任何计算并相应地进行调整

Sure, back when games where done without a engine that was the simplest way to calculate collision detection, 当然,当游戏在没有引擎的情况下完成,这是计算碰撞检测的最简单方法时,

but you will have to take in to account a lot of variables and how they change. 但您必须考虑很多变量及其变化方式。 basically if you wanna know where the player is going to be you have to use the simplest of math, addition. 基本上,如果您想知道玩家的位置,则必须使用最简单的数学加法。 calculating where the player is going to be you have to take the vector and add the velocity. 要计算玩家的位置,您必须采用向量并增加速度。

so position+(Vector3.forward) in a update function would make it increase Vector3(0,0,n) with each frame. 因此更新功能中的position +(Vector3.forward)将使其每帧增加Vector3(0,0,n)。

void Update() {
   Vector3 newpos = transform.position + Vector3.forward;
   transform.position = newpos;
}

In short that is what the code would have to do, if we use Vector3.forward, but you will most likely use your own variables such as 简而言之,如果我们使用Vector3.forward,这就是代码要做的,但是您很可能会使用自己的变量,例如

Vector3 moveForward = new Vector3(0,0,Input.GetAxis("Horizontal"));
moveForward += transform.position;

if that is called after the move has been performed that frame, you will get the position on the forward axix ( you'll also have to add from side to side and up and down i guess ) but its the position of the player next frame, so you can check if a wall is there and if so dont allow player to move forward next frame, which is old school collision detection. 如果在执行完该帧之后调用了该命令,则您将获得前轴的位置(我想您还必须从一侧到另一侧上下添加),但是下一帧玩家的位置,因此您可以检查是否有墙,如果不允许,则不要让玩家向前移动下一帧,这是老式碰撞检测。

I hope that gave some clarity. 我希望这能使您更清楚。

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

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