- Home /
Push player away from the enemy
I've made my own controller for the game I'm trying to make but I've ran into some issues with it. Whenever the enemy is approaching me and I don't stop my character in time, my character teleports on top of the enemy's head.
Push away:
void OnControllerColliderHit(ControllerColliderHit hit)
{
if (hit.gameObject.GetComponent<EnemyController>())
{
Debug.Log("!");
float pushPower = 0.05f;
Vector3 pushDir = hit.transform.forward;
characterController.Move(pushDir * pushPower);
}
}
Movement:
void FixedUpdate()
{
RaycastHit hitInfo;
Physics.SphereCast(transform.position, characterController.radius, Vector3.down, out hitInfo, characterController.height / 2f);
desiredMove = Vector3.ProjectOnPlane(desiredMove, hitInfo.normal).normalized;
desiredMove = (transform.forward * Input.GetAxis("Vertical") + transform.right * Input.GetAxis("Horizontal")).normalized;
if(characterController.isGrounded)
{
moveDir.x = desiredMove.x * speed;
moveDir.z = desiredMove.z * speed;
isJumping = false;
if(Input.GetButtonDown("Jump"))
{
moveDir.y = jumpPower;
isWalking = false;
isJumping = true;
canRun = false;
}
if(Input.GetButton("Sprint") && canRun)
{
isWalking = false;
isRunning = true;
moveDir.x = desiredMove.x * runSpeed;
moveDir.z = desiredMove.z * runSpeed;
}
else
{
isRunning = false;
}
}
else
{
Falling();
}
characterController.Move(moveDir * Time.fixedDeltaTime);
}
I've decided to add a function to push player away if he's too close to the enemy but I can't really get it to work right. Right now it pushes player in the forward direction of the enemy so it works fine when you're approaching the enemy from the front, in other cases my player just teleports in front of the enemy. I wanted to push the player away to the direction he came from but I can't really think of any way to do it. Could anyone help me out with it?
You could try using Rigidbody.AddForce
with Force$$anonymous$$ode.Impulse
with the difference between your player and the enemy's position, something like this:
GetComponent<Rigidbody>().AddForce((transform.position - enemy.transform.position).normalized * pushAmount, Force$$anonymous$$ode.Impulse);
Now I just typed this in the comment, so there are a few things that could go wrong.. You may need to reverse the transform.position
and the enemy.transform.position
, just switch them around if it doesn't push you backward.
It's untested, but that could work.
Another idea is you could just simply only move your character as long as there is no collisions with an enemy. You could set your enemy's tag to a new tag called "Enemy", and do something like this:
bool isColliding;
void OnCollisionEnter(Collision col) {
if(Collision.CompareTag("Enemy"))
isColliding = true;
}
void OnCollisionExit(Collision col) {
if(Collision.CompareTag("Enemy"))
isColliding = false;
}
Now again, I probably messed it up. You may need to replace collision
with collider
, and make sure You OR the enemy have a rigidbody.
Good luck.
Answer by Greyeeh · Aug 21, 2015 at 07:02 PM
Here's the final code:
void OnControllerColliderHit(ControllerColliderHit hit)
{
if (hit.gameObject.GetComponent<EnemyController>())
{
float pushPower = 0.05f;
Vector3 pushDir = transform.position - hit.transform.position;
characterController.Move(pushDir * pushPower);
}
}
Your answer
Follow this Question
Related Questions
Moving a CharacterController Object together with a RigidBody Object 1 Answer
Distribute terrain in zones 3 Answers
Convert Rigidbody code to CharacterController usable 0 Answers
Collision on character controller (fps player) 0 Answers
Having problems with animation and character controller velocity not matching 0 Answers