// Update is called once per frame protected virtual void Update() { OnUpdateRendered(); if (!IsRendered) { return; } if (_isMoving) { Vector3 pos = transform.position; pos = Vector3.MoveTowards(pos, _moveTarget, _moveSpeed * Time.deltaTime); pos.y = SceneObj.SampleHeight(pos); transform.position = pos; // 到达在旋转之前判断 // 若到达则无需旋转,避免零距离旋转误差可能出现的错误 Vector3 step = transform.position - _moveTarget; if (Mathf.Abs(step.x) <= 0.1f && Mathf.Abs(step.y) <= 0.1f && Mathf.Abs(step.z) <= 0.1f) { _remainIdleTime = GetNewIdleTime(); StopMove(); } else { Vector3 dir = _moveTarget - transform.position; dir.y = 0; Quaternion dirRot = Quaternion.LookRotation(dir); Quaternion rot = transform.rotation; rot = Quaternion.RotateTowards(rot, dirRot, _rotateSpeed * Time.deltaTime); transform.rotation = rot; } } else { _remainIdleTime -= Time.deltaTime; if (_remainIdleTime < 0) { _moveTarget = _posFinder.GetNewTarget(); _moveTarget.y = SceneObj.SampleHeight(_moveTarget); StartMove(); } } }
public virtual void Start() { if (SceneObj == null) { return; } _originalPos = transform.position; _originalPos.y = SceneObj.SampleHeight(_originalPos); transform.position = _originalPos; _isMoving = false; _remainIdleTime = GetNewIdleTime(); _renderers = gameObject.GetComponentsInChildren <Renderer>(); foreach (var item in _renderers) { item.receiveShadows = false; } _posFinder = new AIWanderPosFinder(this, _wanderRadius * 0.2f, _wanderRadius * 0.5f); cacheTrans = transform; StopMove(); }