Search Unity

Enemy not Pushing Back Player

Discussion in 'Physics' started by Zerith, May 18, 2020.

  1. Zerith

    Zerith

    Joined:
    Apr 14, 2020
    Posts:
    6
    Hey guys! I'm trying to make the enemy push back the player when the player runs into the enemy. For some reason it is asking me to apply a extreme amount of force to see any effect. I have tried pushing as a trigger instead. All the enemies are tagged enemy and do deplete health.
    using System.Collections;
    using System.Collections.Generic;
    using Unity.Collections.LowLevel.Unsafe;
    using UnityEngine;

    public class PlayerMovement : MonoBehaviour
    {
    [SerializeField] private Rigidbody playerBody;
    [SerializeField] private Vector3 inputVector;
    [SerializeField] private float speed = 5;
    [SerializeField] private float turnSpeed = 45;
    [SerializeField] private float jumpForce = 10f;
    [SerializeField] private bool isOnGround;
    [SerializeField] private float playerHitPoints = 100;
    [SerializeField] float enemyPushForce = 100;





    // Start is called before the first frame update
    void Start()
    {
    //Just making sure we have the rigid body of the game object the script is attached to so we can move it later
    playerBody = gameObject.GetComponent<Rigidbody>();

    }

    // Update is called once per frame
    //This is where the player script should be realizing we are using inputs
    void Update()
    {
    float horizantalInput = Input.GetAxis("Horizontal");
    float forwardInput = Input.GetAxis("Vertical");
    //We move the vehical forward
    transform.Translate(Vector3.forward * Time.deltaTime * speed * forwardInput);
    //We turn the vehical
    transform.Rotate(Vector3.up * Time.deltaTime * turnSpeed * horizantalInput);

    if (Input.GetButtonDown("Jump") && isOnGround)
    {
    playerBody.AddForce(Vector3.up * jumpForce);
    print("Space has been pressed");
    isOnGround = false;
    }



    }

    private void OnCollisionEnter(Collision collision)
    {
    isOnGround = true;
    if (collision.gameObject.tag == "Enemy")
    {
    Debug.Log("Player ran into an enemy");
    playerHitPoints -= 30;
    playerBody.AddForce((collision.gameObject.transform.position - transform.position) * enemyPushForce, ForceMode.Impulse);
    }
    }

    }
    upload_2020-5-17_20-37-3.png