统一运动问题-绕圈跑

时间:2018-09-10 19:20:23

标签: c# unity3d

我在使用移动脚本时遇到问题(Unity版本4.5.5中的Moba Game。我的角色到达了点击的位置,但是当他到达该位置时,他将不会停止!他正在围绕该位置的文字圈中奔跑,

我目前错过了什么?

我制作了以下视频:enter link description here

using UnityEngine; 
using System;
using System.Collections;
using System.Collections.Generic;
using UICommon; 
using BlGame.GameEntity;
using BlGame.GameData;
using JT.FWW.GameData;
using BlGame.FSM;
using BlGame.GuideDate;
using BlGame.Model;
using BlGame.GameState;

[DisallowMultipleComponent]
public class PlayerMovement : MonoBehaviour {
[SerializeField] [Range(1, 20)]
private float speed = 10;

public float stoppingDistance = 0.5f;

private Vector3 targetPosition;
private bool isMoving;

const int LEFT_MOUSE_BUTTON = 1;

private Vector3 orignalPos = new Vector3();
// Use this for initialization
void Start ()
{

    targetPosition = transform.position;
    isMoving = false;

}

// Update is called once per frame
void Update ()
{

    if (Input.GetMouseButton(LEFT_MOUSE_BUTTON))
        SetTargetPosition();

    if (isMoving)
        MovePlayer();
}

void SetTargetPosition()
{
    Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    RaycastHit hit;
    if (Physics.Raycast(ray, out hit))
    {
        targetPosition = new Vector3(hit.point.x, 0, hit.point.z);
        isMoving = true;
    }
}

void MovePlayer()
{

    if (AtTaretPoistion()) {
        isMoving = false;
        SendStop ();
    }
    else {
        SendMove ();
    }

    Debug.DrawLine(transform.position, targetPosition, Color.red);
}

void SendStop(){
    CGLCtrl_GameLogic.Instance.EmsgToss_AskStopMove ();
}

private const float SendMoveInterVal = 0.05f;
private float MoveSendTime;
void SendMove()
{ 
    Vector3 direction = GetTargetDirection();   
    Transform target = JxBlGame.Instance.transform; 
    Entity entity = PlayerManager.Instance.LocalPlayer.RealEntity;
    if (entity == null) {
        return ;        
    }

    PlayState playState = GameStateManager.Instance.GetCurState() as PlayState;
    if (playState == null)
        return;


    target.position = entity.transform.position;
    target.LookAt(entity.transform.position + direction);

    //Positive direction of movement
    Vector3 dir = new Vector3(0, 0, 0);

    //Map has been modified, no rotation required
    //target.transform.Rotate(new Vector3(0.0f, 45f, 0.0f));

    //Get the current camera type
    if (playState.mCameraType == 1)         //Oblique 45 degrees
    {
        //Switch to camera coordinate system
        if (entity.CampType == EntityCampType.CampTypeA)
        {
            Quaternion rot = Quaternion.Euler(0, 45f, 0);
            dir = rot * target.forward;
        }
        else if (entity.CampType == EntityCampType.CampTypeB)
        {
            Quaternion rot = Quaternion.Euler(0, -135f, 0);
            dir = rot * target.forward;
        }
        else
        {
            Debug.Log("no valid entity camp type!");
        }
    }
    else                                 //Level
    {
        //Switch to camera coordinate system
        if (entity.CampType == EntityCampType.CampTypeA)
        {
            Quaternion rot = Quaternion.Euler(0, 0f, 0);
            dir = rot * target.forward;
        }
        else if (entity.CampType == EntityCampType.CampTypeB)
        {
            Quaternion rot = Quaternion.Euler(0, -180f, 0);
            dir = rot * target.forward;
        }
        else
        {
            Debug.Log("no valid entity camp type!");
        }
    }

    Vector3 dealPos = entity.transform.position + dir * Time.deltaTime * PlayerManager.Instance.LocalPlayer.EntityFSMMoveSpeed;
    Vector3 dealPos1 = dealPos + dir * Time.deltaTime * PlayerManager.Instance.LocalPlayer.EntityFSMMoveSpeed * 2;       


    if(dir != Vector3.zero && Time.time - MoveSendTime >= SendMoveInterVal)
    {           
        MoveSendTime = Time.time; 
        CGLCtrl_GameLogic.Instance.EmsgToss_AskMoveDir(dir);
        PlayerAdMove(dir);
    }
}

private void PlayerAdMove(Vector3 mvDir) {
    Iselfplayer player = PlayerManager.Instance.LocalPlayer;
    if (BlGame.Skill.BuffManager.Instance.isHaveStopBuff(player.GameObjGUID))
    {
        return;
    }
    if (player.FSM == null)
    {
        return;
    }
    if (player.FSM.State == FsmState.FSM_STATE_DEAD || player.FSM.State == FsmState.FSM_STATE_RUN || player.FSM.State == FsmState.FSM_STATE_FORCEMOVE
        || player.FSM.State == FsmState.FSM_STATE_RELIVE)
    {
        return;
    }
    float mvSpeed = player.EntityFSMMoveSpeed;
    if(mvSpeed <= 0)
    {
        mvSpeed = 3.0f;
    }
    player.EntityFSMChangedata(player.realObject.transform.position, mvDir, mvSpeed);
    player.OnFSMStateChange(PlayerAdMoveFSM.Instance);
}

Vector3 GetTargetDirection()
{
    Vector3 direction = targetPosition - transform.position;

    direction = new Vector3(direction.x, 0f, direction.z);
    direction.Normalize();

    return direction;
}

private bool AtTaretPoistion(){
    Vector3 start = new Vector3 (transform.position.x, 0, transform.position.z);
    Vector3 end = new Vector3 (targetPosition.x, 0, targetPosition.z);
    return Vector3.Distance (start, end) < stoppingDistance;
}
}

0 个答案:

没有答案