/// <summary> /// 向量之间的距离float /// </summary> public static float Distance(Vector2Float a, Vector2Float b) { Vector2Float vector = a - b; return(vector.Magnitude); }
/// <summary> /// 构造 不要new /// </summary> public CharacterController() { FPosition = Vector2Float.Zero; Speed = 10; }
/// <summary> /// Update /// </summary> public override void Update() { int x = Input.GetDirectInput(ConsoleKey.A, ConsoleKey.D); int y = Input.GetDirectInput(ConsoleKey.S, ConsoleKey.W); input = new Vector2Float(x, y); if (input == Vector2Float.Zero) { FPosition = Vector2Float.Zero; } //启动的时候会送半程的瞬移加速,使反应更灵敏 //理论上来讲间或的按键可能会比连续按住移动更快... else if (input != Vector2Float.Zero && lastInput == Vector2Float.Zero) { FPosition = input * 0.5f; FPosition += input.Normalized * Speed * Time.DeltaTime; } else { FPosition += input.Normalized * Speed * Time.DeltaTime; //如果为zero,则朝向依旧保持原本的朝向,如果输入了按键,那么改变朝向 Direction = (Vector2)input; } //浮点溢出之后进行移动 相当于原来的Rigid int dx = 0, dy = 0; if (FPosition.X > 1) { FPosition.X -= 1; dx += 1; } else if (FPosition.X < -1) { FPosition.X += 1; dx -= 1; } if (FPosition.Y > 1) { FPosition.Y -= 1; dy += 1; } else if (FPosition.Y < -1) { FPosition.Y += 1; dy -= 1; } Vector2 tomove = new Vector2(dx, dy); //如果开启了连续碰撞模式,或者真的产生了位移,才会发送位移请求 if (tomove != Vector2.Zero) { if (!CanMoveInCollider) { //先去物理系统检测是否可以移动,检测的过程中不会移动物体,但是会产生碰撞回调 if (!RuntimeEngine.GetSystem <CollisionSystem>().CanMoveInPos(GetComponent <Collider>(), Position, Position + tomove)) { FPosition = Vector2Float.Zero; } //接下来进行移动 else { Position += tomove; } } else { Position += tomove; } } lastInput = input; }
/// <summary> /// 算两点之间距离 /// </summary> /// <param name="other">另一个对象</param> /// <returns>距离</returns> public float Distance(Vector2Float other) { Vector2Float vector2Float = this - other; return(vector2Float.Magnitude); }