public void MovementUpdate() { lastOnGround = onGround; onGround = checkIfGrounded(); if (onGround && velocity.y <= 0) { velocity.y = 0; velocity.x *= (1 - groundFriction); } else { velocity.x *= (1 - airFriction); } if (velocity.y > 0 && hittingCeiling()) { movementControllerScript.Move(new Vector2(0, velocity.y)); velocity.y = 0; } if (!onGround && lastOnGround) { foreach (GameObject go in movementControllerScript.collisionState.thingsIWasStandingOn) { MovementControllerScript mcs = go.GetComponent <MovementControllerScript>(); if (mcs != null) { velocity += mcs.amountMovedLastFrame; } } } velocity.y -= gravity; }
public void MovementUpdate() { bool switched = false; Vector2 delta = waypoints[currentWaypointIndex] - (Vector2)transform.position; if (delta.magnitude < speed) { velocity = delta; nextWaypoint(); switched = true; } else { velocity = delta.normalized * speed; } Vector2 amountMoved = movementControllerScript.Move(velocity); //this goes to the next waypoint if the platform is stopped by something -- it's not clear that this is the right solution if (Mathf.Abs(amountMoved.magnitude) < Mathf.Abs(velocity.magnitude) * .2 && !switched) { if (!movedLastFrame) { nextWaypoint(); } movedLastFrame = false; } else { movedLastFrame = true; } }
private void ServerReceiveMessage(NetworkMessage message) { // CuantasVecesRecibe++; StringMessage msg = new StringMessage(); msg.value = message.ReadMessage <StringMessage> ().value; GCS.SetPressedButton(msg.value); //SEND MOVEMENT MCS.Move(); //SEND SHOOT if (msg.value == "L") { GCS.isShooting = true; } // if (GCS.getGameover ()) { // if (msg.value == "C") { // GCS.isShooting = true; // } // //RESTART GAME // } // // if (GCS.getGameWon ()) { // if (msg.value == "Z") { // GCS.isShooting = true; // } }
// Update is called once per frame void LateUpdate() { if (affectedByGravity) { velocity.y -= gravity; } movementControllerScript.Move(velocity); onGround = checkIfGrounded(); onCeiling = checkIfCeilinged(); touchWall = checkIfWalled(); if (affectedByFriction) { if (onGround) { velocity.y = 0; velocity.x *= (1 - groundFriction); } else { velocity.x *= (1 - airFriction); } } if (onCeiling) { velocity.y = 0; } if (touchWall) { velocity.x = 0; } }
// Update is called once per frame void Update() { Vector2 delta = waypoints[currentWaypointIndex] - (Vector2)transform.position; if (delta.magnitude < speed) { velocity = delta; nextWaypoint(); } else { velocity = delta.normalized * speed; } movementControllerScript.FindPassengers(); movementControllerScript.Move(velocity); }
public void MovementUpdate() { sprite.flipX = (facing == -1); physicsScript.MovementUpdate(); pusherScript.amIPushing = (pushPressed && physicsScript.onGround && Mathf.Abs(physicsScript.velocity.x) > 0); pusherScript.pushFacing = facing; pusherScript.MovementUpdate(); if (pusherScript.thingIAmPushing != null) { pusherScript.thingIAmPushing.GetComponent <MovementControllerScript>().Move(new Vector2(physicsScript.velocity.x, 0)); } movementControllerScript.Move(physicsScript.velocity); if (pusherScript.thingIAmPulling != null) { pusherScript.thingIAmPulling.GetComponent <MovementControllerScript>().Move(new Vector2(physicsScript.velocity.x, 0)); } }
public void MovementUpdate() { physicsScript.MovementUpdate(); movementControllerScript.Move(physicsScript.velocity); }
public Vector2 Move(Vector2 delta, Vector2 sourcePusherLevel, int depthCount) { depthCount++; if (depthCount > 10) { return(new Vector2(0, 0)); } else { //Move attempts to move in the x dimension and then the y //it only moves us as far as we can move, and returns however far that was float direction = 0; Vector2 amountLeftToMove = delta; Vector2 totalAmountMoved = new Vector2(); List <RaycastHit2D> currentPushHits = new List <RaycastHit2D>(); List <RaycastHit2D> currentCarryHits = new List <RaycastHit2D>(); List <MovementControllerScript> hitThingsXmcs = new List <MovementControllerScript>(); List <MovementControllerScript> hitThingsYmcs = new List <MovementControllerScript>(); for (int i = 0; i < numberOfPassengerMovementPasses; i++) { Vector2 amountMoved = new Vector2(0, 0); if (Mathf.Abs(amountLeftToMove.x) > 0) { direction = Mathf.Sign(amountLeftToMove.x); currentPushHits = HorizontalRaycastHits(MIN_DISTANCE * direction); RaycastHit2D closestHit = GetClosestHit(currentPushHits); if (closestHit.transform != null) { MovementControllerScript mcs = closestHit.transform.gameObject.GetComponent <MovementControllerScript>(); if (mcs != null) { bool shouldGetPushed = (mcs.pushableLevel.x < sourcePusherLevel.x); PhysicsScript physicsScript = mcs.GetComponent <PhysicsScript>(); if (physicsScript != null) { if (physicsScript.onGround == false || physicsScript.velocity.y > 0) //this is a convulted way of checking if I'm in the air and therefore should be pushed { shouldGetPushed = true; } } if (shouldGetPushed) { hitThingsXmcs.Add(mcs); mcs.Move(new Vector2(amountLeftToMove.x, 0), sourcePusherLevel, depthCount); } } } amountMoved.x = CalculateMoveX(amountLeftToMove); List <GameObject> carryObjects = new List <GameObject>(); currentCarryHits = VerticalRaycastHits(MIN_DISTANCE); foreach (RaycastHit2D hit in currentCarryHits) { if (hit.transform != null) { if (!carryObjects.Contains(hit.transform.gameObject)) { if (hit.transform.gameObject.GetComponent <PhysicsScript>() != null) { carryObjects.Add(hit.transform.gameObject); } } } } if (carryObjects.Count > 0 && amountMoved.x != 0) { List <GameObject> carryObjectsSorted = SortGameObjectsByPositionX(carryObjects); int iStart; int iDirection; int iEnd; if (amountMoved.x < 0) { iStart = 0; iDirection = 1; iEnd = carryObjectsSorted.Count; } else { iStart = carryObjectsSorted.Count - 1; iDirection = -1; iEnd = -1; } for (int j = iStart; j != iEnd; j += iDirection) { GameObject carryObject = carryObjectsSorted[j]; MovementControllerScript mcs = carryObject.GetComponent <MovementControllerScript>(); if (mcs != null) { if (!mcs.beenCarriedThisFrame) { mcs.beenCarriedThisFrame = true; mcs.Move(new Vector2(amountMoved.x, 0)); } } } } } TranslateAndRecord(amountMoved); if (Mathf.Abs(amountLeftToMove.y) > 0) { direction = Mathf.Sign(amountLeftToMove.y); currentPushHits = VerticalRaycastHits(MIN_DISTANCE * direction); List <GameObject> hitObjects = new List <GameObject>(); foreach (RaycastHit2D hit in currentPushHits) { if (hit.transform != null) { if (!hitObjects.Contains(hit.transform.gameObject)) { hitObjects.Add(hit.transform.gameObject); } } } List <GameObject> pushedObjects = new List <GameObject>(); foreach (GameObject hitObject in hitObjects) { MovementControllerScript hitMCS = hitObject.GetComponent <MovementControllerScript>(); if (hitMCS != null) { if (hitMCS.pushableLevel.y < sourcePusherLevel.y) { if (!pushedObjects.Contains(hitMCS.gameObject)) { pushedObjects.Add(hitMCS.gameObject); } } } } float minDistanceMoved = Mathf.Infinity; float[] distancesMoved = new float[pushedObjects.Count]; for (int j = 0; j < pushedObjects.Count; j++) { GameObject pushedObject = pushedObjects[j]; MovementControllerScript hitMCS = pushedObject.GetComponent <MovementControllerScript>(); distancesMoved[j] = hitMCS.Move(new Vector2(0, amountLeftToMove.y), sourcePusherLevel, depthCount).y; if (distancesMoved[j] < minDistanceMoved) { minDistanceMoved = distancesMoved[j]; } } for (int j = 0; j < pushedObjects.Count; j++) { GameObject pushedObject = pushedObjects[j]; if (distancesMoved[j] > minDistanceMoved) { MovementControllerScript mcs = pushedObject.GetComponent <MovementControllerScript>(); if (mcs != null) { float diff = distancesMoved[j] - minDistanceMoved; mcs.Move(new Vector2(0, diff * direction * -1)); } } } List <GameObject> carryObjects = new List <GameObject>(); if (direction < 0) { currentCarryHits = VerticalRaycastHits(MIN_DISTANCE); foreach (RaycastHit2D hit in currentCarryHits) { if (hit.transform != null) { if (!carryObjects.Contains(hit.transform.gameObject)) { carryObjects.Add(hit.transform.gameObject); } } } } amountMoved.y = CalculateMoveY(amountLeftToMove); TranslateAndRecord(new Vector2(0, amountMoved.y)); if (direction < 0) { foreach (GameObject carryObject in carryObjects) { MovementControllerScript mcs = carryObject.GetComponent <MovementControllerScript>(); PhysicsScript physicsScript = carryObject.GetComponent <PhysicsScript>(); //TODO: this is not the right way to prevent things from simultaneously pulling objects down and being pushed by them if (physicsScript != null) { if (mcs.pushableLevel.y <= pusherLevel.y) { mcs.Move(new Vector2(0, amountMoved.y)); } } } } } amountLeftToMove -= amountMoved; totalAmountMoved += amountMoved; } return(totalAmountMoved); } }
public Vector2 Move(Vector2 delta) { Vector2 amountMoved; if (attachedObjects.Count > 0) { amountMoved = MoveWithGameObjects(delta, attachedObjects); } else { //Move attempts to move in the x dimension and then the y //it only moves us as far as we can move, and returns however far that was Vector2 newDelta = new Vector2(delta.x, delta.y); if (delta.x != 0) { float slopeAngle = getSlopeUpAngle(Mathf.Sign(delta.x)); if (slopeAngle < maxAscentAngle) { float slopeY = Mathf.Sin(slopeAngle * Mathf.Deg2Rad) * Mathf.Abs(delta.x); Move(new Vector2(0, slopeY)); Debug.Log("up is" + slopeAngle); ascendingSlope = true; } else { ascendingSlope = false; } UpdateCollisionState(); newDelta.x = CalculateMoveX(delta); } transform.Translate(new Vector2(newDelta.x, 0)); if (delta.y != 0) { float slopeAngle = getSlopeDownAngle(Mathf.Tan(delta.y)); if (delta.y != 0) { float slopeX = Mathf.Sin(slopeAngle * Mathf.Deg2Rad) * (delta.y); Move(new Vector2(-slopeX, 0)); Debug.Log("down is" + slopeAngle); decendingSlope = true; } else { decendingSlope = false; } UpdateCollisionState(); newDelta.y = CalculateMoveY(delta); } amountMoved = newDelta; transform.Translate(new Vector2(0, newDelta.y)); } foreach (GameObject passenger in passengers) { MovementControllerScript mcs = passenger.GetComponent <MovementControllerScript>(); if (mcs != null) { mcs.Move(amountMoved); } } return(amountMoved); }