- Home /
How can I make a block push the player?
Hi, I want to make a block that can push the player off of a ledge. I have already made the block move using the Vector3.MoveTowards function and it seems to move basic rigid body cubes normally but when it comes to the character controller it just phases right through. Here is the code I'm working with.
Any help would be apreshiated.
void Update()
{
buttonPressedState = myButton.GetComponent<ButtonBehaviour>().isPressed;
if (isPositionSwap)
{
if (buttonPressedState == swapStateGoal)
{ //if the button is pressed, start the move and swap the desired button output
isMoving = true;
if (swapStateGoal)
{
swapStateGoal = false;
}
else
{
swapStateGoal = true;
}
}
if (isMoving)
{
if (Vector3.Distance(transform.position, targetPosition[currentTargetPosArrIndex].transform.position) > 0.1)
{ // if not close enough, move closer
platNewPos = Vector3.MoveTowards(transform.position, targetPosition[currentTargetPosArrIndex].transform.position, moveSpeed * Time.deltaTime);
platMovement = platNewPos - transform.position;
transform.position = platNewPos;
}
else
{
// close enough swap target position
currentTargetPosArrIndex++;
if (currentTargetPosArrIndex > targetPosition.Length -1)
{
currentTargetPosArrIndex = 0;
}
isMoving = false;
}
}
}
else
{
if (buttonPressedState) // when button is pressed
{
if (Vector3.Distance(transform.position, targetPosition[currentTargetPosArrIndex].transform.position) > 0.1)
{ // if not close enough, move closer
platNewPos = Vector3.MoveTowards(transform.position, targetPosition[currentTargetPosArrIndex].transform.position, moveSpeed * Time.deltaTime);
platMovement = platNewPos - transform.position;
transform.position = platNewPos;
}
else
{
// close enough swap target position
currentTargetPosArrIndex++;
if (currentTargetPosArrIndex > targetPosition.Length -1)
{
currentTargetPosArrIndex = 0;
}
}
}
}
}
Answer by Vollmondum · Apr 19, 2018 at 11:40 AM
Move towards ignores colliders. 1. Player needs to have a rigidbody 2. Player's (or cube's) collider should be thick enough for the cube to react on collision, until its center crossed player's
Answer by Disthron · Apr 19, 2018 at 11:48 AM
Thanks for your response!
Move towards ignores colliders Hmm... what about Lerp, does that ignore colliders too. Also, whey does it work for the basic block but not the player controller.
There's number of ways to achieve the goal. $$anonymous$$ost people would suggest AddForce to any collidable thingies. I personally prefer $$anonymous$$oveTowards and increase collider's diameter while moving, it moves smoother that way. Lerp is great for camera movement only, considering Lerping something barely reaches final destination coordinate, say while lerping to 1.0, it comes as close as 0.999999, but still not 1, and that's when you don't see a reason for things not to work, since 0.999999 != 1 :)
Player Controller is basic thing with its own mechanics and completed scripting. I call it "lazy controller". Drag into the scene and no need for coding. Can't answer about native player controller for sure. I code controllers from scratch