private void FixedUpdate() { //upAxis = -Physics.gravity.normalized; //5.2.2 自定义重力 Vector3 gravity = CustomGravity.GetGravity(body.position, out upAxis); //velocity = body.velocity; UpdateState(); AdjustVelocity(); ////float maxSpeedChange = maxAcceleration * Time.deltaTime; //float acceleration = onGround ? maxAcceleration : maxAirAcceleration; //float maxSpeedChange = acceleration * Time.deltaTime; ////通过当前加速度逼近目标速度 //velocity.x = Mathf.MoveTowards(velocity.x, desiredVelocity.x, maxSpeedChange); //velocity.z = Mathf.MoveTowards(velocity.z, desiredVelocity.z, maxSpeedChange); if (desiredJump) { desiredJump = false; Jump(gravity); } if (Climbing) { velocity -= contactNormal * (maxClimbAcceleration * 0.9f * Time.deltaTime); } else if (OnGround && velocity.sqrMagnitude < 0.01f) { velocity += contactNormal * (Vector3.Dot(gravity, contactNormal) * Time.deltaTime); } else if (desiresClimbing && OnGround) { velocity += (gravity - contactNormal * (maxClimbAcceleration * 0.9f)) * Time.deltaTime; } else { velocity += gravity * Time.deltaTime; } //控制刚体速度 body.velocity = velocity; //onGround = false; ClearState(); }
void FixedUpdate() { Vector3 gravity = CustomGravity.GetGravity(body.position, out upAxis); UpdateState(); if (InWater) { velocity *= 1f - waterDrag * submergence * Time.deltaTime; } AdjustVelocity(); if (desiredJump) { desiredJump = false; Jump(gravity); } if (Climbing) { velocity -= contactNormal * (maxClimbAcceleration * 0.9f * Time.deltaTime); } else if (InWater) { velocity += gravity * ((1f - buoyancy * submergence) * Time.deltaTime); } else if (OnGround && velocity.sqrMagnitude < 0.01f) { velocity += contactNormal * (Vector3.Dot(gravity, contactNormal) * Time.deltaTime); } else if (desiresClimbing && OnGround) { velocity += (gravity - contactNormal * (maxClimbAcceleration * 0.9f)) * Time.deltaTime; } else { velocity += gravity * Time.deltaTime; } body.velocity = velocity; ClearState(); }
void FixedUpdate() { if (floatToSleep) { if (body.IsSleeping()) { floatDelay = 0f; return; } if (body.velocity.sqrMagnitude < 0.0001f) { floatDelay += Time.deltaTime; if (floatDelay >= 1f) { return; } } else { floatDelay = 0f; } } gravity = CustomGravity.GetGravity(body.position); float dragFactor = waterDrag * Time.deltaTime / buoyancyOffsets.Length; float buoyancyFactor = -buoyancy / buoyancyOffsets.Length; for (int i = 0; i < buoyancyOffsets.Length; i++) { if (submergence[i] > 0f) { float drag = Mathf.Max(0f, 1f - dragFactor * submergence[i]); body.velocity *= drag; body.angularVelocity *= drag; body.AddForceAtPosition( gravity * (buoyancyFactor * submergence[i]), transform.TransformPoint(buoyancyOffsets[i]), ForceMode.Acceleration ); submergence[i] = 0f; } } body.AddForce( gravity, ForceMode.Acceleration ); }
void FixedUpdate() { Vector3 gravity = CustomGravity.GetGravity(body.position, out upAxis); UpdateState(); AdjustVelocity(); if (desiredJump) { desiredJump = false; Jump(gravity); } velocity += gravity * Time.deltaTime; body.velocity = velocity; ClearState(); }
private void FixedUpdate() { gravity = CustomGravity.GetGravity(transform.position, out upAxis); UpdateState(); AdjustRotation(); AdjustVelocity(); if (desiredJump) { desiredJump = false; Jump(); } velocity += gravity * Time.deltaTime; rb.velocity = velocity; rb.rotation = rotation.normalized; ClearState(); }
void FixedUpdate() { Vector3 gravity = CustomGravity.GetGravity(body.position, out worldUp); UpdateState(); AdjustVelocity(); if (desiredJump) { desiredJump = false; Jump(gravity); } velocity += gravity * Time.deltaTime; model.position = transform.position - (worldUp * 0.3f); body.velocity = velocityEnabled ? velocity : Vector3.zero; ClearState(); }
void UpdateGravityAlignment() { Vector3 fromUp = gravityAlignment * Vector3.up; Vector3 toUp = CustomGravity.GetUpAxis(focusPoint); float dot = Mathf.Clamp(Vector3.Dot(fromUp, toUp), -1f, 1f); float angle = Mathf.Acos(dot) * Mathf.Rad2Deg; float maxAngle = upAlignmentSpeed * Time.deltaTime; Quaternion newAlignment = Quaternion.FromToRotation(fromUp, toUp) * gravityAlignment; if (angle <= maxAngle) { gravityAlignment = newAlignment; } else { gravityAlignment = Quaternion.SlerpUnclamped(gravityAlignment, newAlignment, maxAngle / angle); } }
protected void SwitchDirection(Vector3 direction, PlayerRotateBase rotator) { float _angleThreshold = 0.001f; float _angleBetweenUpDirections = Vector3.Angle(direction, rotator.transform.up); if (_angleBetweenUpDirections < _angleThreshold) { return; } Quaternion _rotationDifference = Quaternion.FromToRotation(rotator.transform.up, direction); rotator.Rotate(_rotationDifference); CustomGravity.UnregisterAll(); CustomGravity.Register(this); if (audioclip) { AudioManager.instance.PlayClipAtSource(audioclip, thisTransform.position); } }
// Update is called once per frame void FixedUpdate() { if (floatToSleep) { if (body.IsSleeping()) { floatDelay = 0f; return; } if (body.velocity.sqrMagnitude < 0.0001f) { floatDelay += Time.deltaTime; if (floatDelay >= 1f) { return; } } else { floatDelay = 0f; } } gravity = CustomGravity.GetGravity(body.position); if (submergence > 0f) { float drag = Mathf.Max(0f, 1f - waterDrag * submergence * Time.deltaTime); body.velocity *= drag; body.angularVelocity *= drag; body.AddForce(gravity * -(buoyancy * submergence), ForceMode.Acceleration ); submergence = 0f; } // body.AddForce(gravity, ForceMode.Acceleration); body.AddForceAtPosition(gravity * -(buoyancy * submergence), transform.TransformPoint(buoyancyOffset), ForceMode.Acceleration ); }
GameObject HeroGo() // Hero Generator { GameObject body; // Create a GameObject to Store an Object body = new GameObject(); // Create Parent // RIGIDBODY body.AddComponent<Rigidbody> (); Rigidbody rb = body.GetComponent<Rigidbody> (); rb.mass = 60f; rb.drag = 6f; rb.angularDrag = .05f; rb.interpolation = RigidbodyInterpolation.Interpolate; rb.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic; rb.constraints = RigidbodyConstraints.FreezeRotation; // END RIGIDBODY // COLLIDER body.AddComponent<CapsuleCollider>(); CapsuleCollider cc = body.GetComponent<CapsuleCollider> (); cc.radius = .5f; cc.height = 2f; cc.direction = 1; // 0 = x, 1 = y, 2 = z // END COLLIDER // SCRIPTS body.AddComponent<CustomGravity> (); CustomGravity cg = body.GetComponent<CustomGravity> (); cg.gravityScale = 15; body.AddComponent<FPS_cam> (); body.AddComponent<FPS_move> (); body.AddComponent<HideCursorOnScreen> (); // END SCRIPTS // GUN GameObject gun = GameObject.CreatePrimitive(PrimitiveType.Cube); // Create a Gun gun.name = "Gun"; gun.transform.position = new Vector3 (.38f, -.4f, .4f); gun.transform.localScale = new Vector3 (.28f, .26f, 1f); gun.transform.SetParent (body.transform); // Rig gun.GetComponent<Renderer>().material.SetColor("_Color", Color.yellow); // END GUN return body; }
private void FixedUpdate() { //applies the gravity, changing the up axis Vector3 gravity = CustomGravity.GetGravity(_body.position, out _upAxis); UpdateState(); AdjustVelocity(); AdjustRotationBody(); //applies the jump if the jump button has been pressed since last FixedUpdate if (_desiredJump) { _desiredJump = false; Jump(gravity); } //applies velocity depending of surface being touched if (OnGround && _velocity.sqrMagnitude < 0.01f) { _velocity += _contactNormal * (Vector3.Dot(gravity, _contactNormal) * Time.deltaTime); } else { _velocity += gravity * Time.deltaTime; } //applies fallMultiplier float dotVelocityUp = Vector3.Dot(Vector3.Project(_velocity, _upAxis), _upAxis); if (dotVelocityUp < 0 && !OnGround && !OnSteep) { _velocity += (fallMultiplier - 1) * Time.deltaTime * gravity; } //applies lowJumpMultiplier else if (dotVelocityUp > 0 && !Input.GetButton("Jump") && !OnGround && !OnSteep) { _velocity += (lowJumpMultiplier - 1) * Time.deltaTime * gravity; } _body.velocity = _velocity; ClearState(); }
/// <summary> /// 定时更新 /// </summary> void FixedUpdate() { // 1. 检查如果物体受力平衡,则直接返回不要打扰 if (m_body.IsSleeping()) { m_floatDelay = 0f; return; } // 2. 但是如果前一帧AddForce,则不会进入Sleep()状态,所以首先通过检查速度来决定是否AddForce if (m_body.velocity.sqrMagnitude < 0.0001f) { m_floatDelay += Time.deltaTime; // 3. 当物体已经静止超过一段时间后,视为静止 if (m_floatDelay >= 1f) { //4. 设定休眠颜色并直接返回 GetComponent <MeshRenderer>().material.SetColor("_Color", m_sleepColor); return; } } // 5. 否则设置当前为非休眠颜色 GetComponent <MeshRenderer>().material.SetColor("_Color", m_initialColor); //todo 得到当前位置的重力以及浸入度,并处理受力 m_gravity = CustomGravity.GetGravity(m_body.position); if (m_submergence > 0f) { //todo 从水阻入手计算速度的比例系数(水阻对速度的阻碍成都) float drag = Mathf.Max(0f, 1f - m_waterDrag * m_submergence * Time.deltaTime); //todo 直接*速度 m_body.velocity *= drag; m_body.angularVelocity *= drag; //todo 在浮力应用点(是全局位置)上应用浮力 m_body.AddForceAtPosition( m_gravity * -(m_buoyancy * m_submergence), transform.TransformPoint(m_buoyancyOffset), ForceMode.Acceleration ); m_submergence = 0f; //? 这个直接设置为0不妥了? } m_body.AddForce(m_gravity, ForceMode.Acceleration); }
private void FixedUpdate() { if (body.IsSleeping()) { return; } if (body.velocity.sqrMagnitude < 0.0001f) { delayTime += Time.deltaTime; if (delayTime >= 1f) { return; } } else { delayTime = 0f; } body.AddForce(CustomGravity.GetGravity(body.position), ForceMode.Acceleration); }
void LateUpdate() { gravityAlignment = Quaternion.FromToRotation( gravityAlignment * Vector3.up, CustomGravity.GetUpAxis(focusPoint) ) * gravityAlignment; UpdateFocusPoint(); if (ManualRotation() || AutomaticRotation()) { ConstrainAngles(); orbitRotation = Quaternion.Euler(orbitAngles); } Quaternion lookRotation = gravityAlignment * orbitRotation; Vector3 lookDirection = lookRotation * Vector3.forward; Vector3 lookPosition = focusPoint - lookDirection * distance; Vector3 rectOffset = lookDirection * regularCamera.nearClipPlane; Vector3 rectPosition = lookPosition + rectOffset; Vector3 castFrom = focus.position; Vector3 castLine = rectPosition - castFrom; float castDistance = castLine.magnitude; Vector3 castDirection = castLine / castDistance; if (Physics.BoxCast( castFrom, CameraHalfExtends, castDirection, out RaycastHit hit, lookRotation, castDistance, obstructionMask )) { rectPosition = castFrom + castDirection * hit.distance; lookPosition = rectPosition - rectOffset; } transform.SetPositionAndRotation(lookPosition, lookRotation); }
private void Trigger2DFuntion(Collider2D other) { CustomGravity customGravity = other.GetComponent <CustomGravity>(); _rb.AddForce(customGravity.Direction * customGravity.Strength); currentGravityDirection = customGravity.Direction; if (airBorne) { float dot = Vector2.Dot(customGravity.Direction, _rb.velocity); if ((!_verticallyInverted && dot > 0) || (_verticallyInverted && dot < 0)) { Flip(); } } else { float dot = Vector2.Dot(customGravity.Direction, transform.up); if ((!_verticallyInverted && dot > 0) || (_verticallyInverted && dot < 0)) { Flip(); } } }
void FixedUpdate() { if (floatToSleep) { if (body.IsSleeping()) { floatDelay = 0f; return; } if (body.velocity.sqrMagnitude < 0.0001f) { floatDelay += Time.deltaTime; if (floatDelay >= 1f) { return; } } } body.AddForce(CustomGravity.GetGravity(body.position), ForceMode.Acceleration); }
void OnDisable() { CustomGravity.UnregisterGravitySource(this); }
void OnDisable() { CustomGravity.Unregister(this); }
private void Start() { _rb = GetComponent <Rigidbody>(); _g = GetComponent <CustomGravity>(); }
void OnEnable() { CustomGravity.Register(this); }
/// <summary> /// 按照固定时长被调用,在被推迟之后仍然会调用本应调用的次数 /// </summary> void FixedUpdate() { // 1. 记录当前重力及反方向 Vector3 gravity = CustomGravity.GetGravity(m_body.position, out m_upAxis); // 2. 更新当前物体物理状态为Update做准备 UpdateState(); //todo 如果在水中,则先添加水阻 if (InWater) { // 根据浸没率和水阻、时间计算实际水阻对速度的影响 m_velocity *= 1f - m_waterDrag * m_waterDrag * Time.deltaTime; // 阻力在此添加仍能保证之后允许加速 //? 但是一个绝对数量1和一个时间比例是如何ok的? } // 3. 更新速度 AdjustVelocity(); // 4. 如果之前(Update中)按下了跳跃键 if (m_desiredJump) { // 5. 将“妄图跳跃”重置代表已经进行跳跃 m_desiredJump = false; // 6. 进行跳跃 Jump(gravity); } // todo 如果攀爬 if (Climbing) { //todo 增加一项下压力,用于越过墙壁凸角 m_velocity -= m_contactNormal * (m_maxClimbAcceleration * 0.9f * Time.deltaTime); // 0.9的原因是免得速度抵消被吸到内叫上 } //todo 如果不攀爬则判断是否在水中(攀爬判定优先) else if (InWater) { //todo 根据浸没率及浮力率计算速度的修正 m_velocity += gravity * ((1f - m_submergence * m_buoyancy) * Time.deltaTime); } //todo 如果不在水中则判断是否在地面(水中优先) else if (OnGround && m_velocity.sqrMagnitude < 0.01f) { //todo 增加一项沿斜面法线的下压力,使压力不至于因为这个branch而消失但也不会在很平缓的斜面上缓慢拉动个体 m_velocity += m_contactNormal * Vector3.Dot(gravity, m_contactNormal) * Time.deltaTime; } //todo 如果在地面但同时妄图攀爬 else if (m_desiresClimbing && OnGround) { //todo 增加一项与墙面凸角相关的压力,用于从Ground进入攀援 m_velocity += (gravity - m_contactNormal * (m_maxClimbAcceleration * 0.9f)) * Time.deltaTime; } else { // 7. 在速度变化上叠加重力影响 m_velocity += gravity * Time.deltaTime; } // 8. 更新rigidbody速度 m_body.velocity = m_velocity; // 9. 清空为当前帧其他函数记录的状态 ClearState(); }
// End Attack #region Init public void Init(GameObject body, string name, int age, Text text, Image image) { body.tag = "Player"; body.name = name.ToUpper() + " " + age; // Hero Name // RIGIDBODY body.AddComponent <Rigidbody>(); Rigidbody rb = body.GetComponent <Rigidbody>(); rb.mass = 60f; rb.drag = 6f; rb.angularDrag = .05f; rb.interpolation = RigidbodyInterpolation.Interpolate; rb.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic; rb.constraints = RigidbodyConstraints.FreezeRotation; // END RIGIDBODY // COLLIDER CapsuleCollider cc = body.GetComponent <CapsuleCollider>(); cc.radius = .5f; cc.height = 2f; cc.direction = 1; // 0 = x, 1 = y, 2 = z // END COLLIDER // SCRIPTS body.AddComponent <CustomGravity>(); CustomGravity cg = body.GetComponent <CustomGravity>(); cg.gravityScale = 15; body.AddComponent <FPS_cam>(); body.AddComponent <FPS_move>(); FPS_move movement = body.GetComponent <FPS_move>(); HeroSpeed speed = new HeroSpeed(age); movement.walk = speed.heroSpeed; // Age Speed // END SCRIPTS // GUN GameObject gun = GameObject.CreatePrimitive(PrimitiveType.Cube); // Create a Gun gun.name = "Gun"; gun.transform.position = new Vector3(.4f, -.4f, -.02f); gun.transform.localScale = new Vector3(.28f, .26f, 1f); gun.transform.SetParent(body.transform); // Rig gun.GetComponent <Renderer>().material.SetColor("_Color", Color.yellow); // END GUN // CAM GameObject cam = GameObject.FindGameObjectWithTag("MainCamera"); // Find Main Camera cam.name = "Cam"; cam.transform.SetParent(body.transform); // END CAM // CANVAS messages = text; bg = image; // END CANVAS }
private void LateUpdate() { ////5.1.4 自定义重力下对齐轨道摄像机 //gravityAlignment = Quaternion.FromToRotation(gravityAlignment * Vector3.up, -Physics.gravity.normalized) * gravityAlignment; //5.2.2 应用自定义重力 gravityAlignment = Quaternion.FromToRotation(gravityAlignment * Vector3.up, CustomGravity.GetUpAxis(focusPoint)) * gravityAlignment; ////2.2 控制轨道 ////Vector3 focusPoint = focus.position; //UpdateFocusPoint(); //ManualRotation(); //Quaternion lookRotation = Quaternion.Euler(orbitAngles); ////Vector3 lookDirection = transform.forward; //Vector3 lookDirection = lookRotation * Vector3.forward; ////transform.localPosition = focusPoint - lookDirection * distance; //Vector3 lookPosition = focusPoint - lookDirection * distance; //transform.SetPositionAndRotation(lookPosition, lookRotation); //2.3 约束角度 UpdateFocusPoint(); //Quaternion lookRotation; if (ManualRotation() || AutomaticRotation()) { ConstrainAngles(); orbitRotation = Quaternion.Euler(orbitAngles); } //else //{ // lookRotation = transform.localRotation; //} Quaternion lookRotation = gravityAlignment * orbitRotation; Vector3 lookDirection = lookRotation * Vector3.forward; Vector3 lookPosition = focusPoint - lookDirection * distance; ////4.1减少外观距离 //if (Physics.Raycast(focusPoint, -lookDirection, out RaycastHit hit, distance)) //{ // lookPosition = focusPoint - lookDirection * hit.distance; //} ////4.2保持近平面清晰 采用盒子投射 //if (Physics.BoxCast(focusPoint, CameraHalfExtends, -lookDirection, out RaycastHit hit, lookRotation, distance - regularCamera.nearClipPlane)) //{ // lookPosition = focusPoint - lookDirection * (hit.distance + regularCamera.nearClipPlane); //} //4.3聚焦半径 Vector3 rectOffset = lookDirection * regularCamera.nearClipPlane; Vector3 rectPosition = lookPosition + rectOffset; Vector3 castFrom = focus.position; Vector3 castLine = rectPosition - castFrom; float castDistance = castLine.magnitude; Vector3 castDirection = castLine / castDistance; if (Physics.BoxCast(castFrom, CameraHalfExtends, castDirection, out RaycastHit hit, lookRotation, castDistance, obstructionMask)) { //lookPosition = focusPoint - lookDirection * (hit.distance + regularCamera.nearClipPlane); rectPosition = castFrom + castDirection * hit.distance; lookPosition = rectPosition - rectOffset; } transform.SetPositionAndRotation(lookPosition, lookRotation); }
void Start() { SaveObject = GameObject.Find("SaveObject").GetComponent<SaveScript>(); playerClone = GameObject.Find("Player_Rotation"); Direction = Vector3.forward; customGravity = GetComponent<CustomGravity>(); StartCoroutine(AI()); }
private void FixedUpdate() { // _upAxis = -Physics.gravity.normalized; Vector3 gravity = CustomGravity.GetGravity(body.position, out upAxis); UpdateState(); if (InWater) { velocity *= 1f - waterDrag * submergence * Time.deltaTime; } AdjustVelocity(); if (desiredJump) { desiredJump = false; Jump(gravity); } if (Climbing) { velocity -= contactNormal * (maxClimbAcceleration * 0.8f * Time.fixedDeltaTime); } else if (InWater) { velocity += gravity * ((1f - buoyancy * submergence) * Time.deltaTime); } else if (OnGround && velocity.sqrMagnitude < 0.01f) { velocity += contactNormal * (Vector3.Dot(gravity, contactNormal) * Time.deltaTime); } else if (desiresClimbing && OnGround) { velocity += (gravity - contactNormal * (maxClimbAcceleration * 0.8f)) * Time.fixedDeltaTime; } else { velocity += gravity * Time.fixedDeltaTime; } body.velocity = velocity; ///////// TODO hacer esto en las BehaviourScripts animator.SetBool("TargetLock", lockDir); if (body.velocity.sqrMagnitude / maxSpeed > 0.02) { if (!lockDir) { maxSpeed = 10; animator.speed = 1; animator.SetFloat("VelocityY", body.velocity.magnitude / maxSpeed); animator.SetFloat("VelocityX", 0f); } else { maxSpeed = 5; animator.speed = 1.5f; animator.SetFloat("VelocityY", body.velocity.magnitude / maxSpeed); animator.SetFloat("VelocityX", body.velocity.magnitude / maxSpeed); } } else { animator.SetFloat("VelocityY", 0f); animator.SetFloat("VelocityX", 0f); } ////////////////////////////////////////// AlignBody(); ClearState(); }
// Start is called before the first frame update void Start() { customGravity = GetComponent <CustomGravity>(); }
void Start() { actualDirection = 0; switch (directions[actualDirection]) { case "Left": Direction = Vector3.left; break; case "Right": Direction = Vector3.right; break; case "Forward": case "Up": Direction = Vector3.forward; break; case "Back": case "Down": case "Backwards": Direction = Vector3.back; break; } actualDirection ++; customGravity = GetComponent<CustomGravity>(); StartCoroutine(AI()); }
void OnEnable() { CustomGravity.RegisterGravitySource(this); }
/// <summary> /// 当该物体不可用时调用 /// </summary> void OnDisabled() { // 去掉重力源的注册 CustomGravity.Unregister(this); }
/// <summary> /// 当该物体可用时调用 /// </summary> void OnEnable() { // 将该重力源注册至Gravity的处理类 CustomGravity.Register(this); }
void Start() { switch (ainDirection.ToLower ()) { case "left": Direction = Vector3.left; break; case "right": Direction = Vector3.right; break; case "forward": case "up": case "front": Direction = Vector3.forward; break; case "back": case "backwards": case "down": Direction = Vector3.back; break; default: Direction = Vector3.back; break; } customGravity = GetComponent<CustomGravity>(); StartCoroutine(AI()); }