#Trying collision

1 messages · Page 1 of 1 (latest)

little compass
#

Trying to make my player collide with the enemy and bounce back

#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMoves : MonoBehaviour
{
    public CharacterController controller;
    public float speed = 12f;
    public float gravity = -9.81f;
    public float jumpHeight = 3f;
 
    public Transform groundCheck;
    public float groundDistance = 0.4f;
    public LayerMask groundMask;

    public float KBF = 50; //KnockbackForce
    public GameObject Base_Enemy;
    public Rigidbody rb1;
    public Vector3 moveDirectionPush;

    Vector3 velocity;
    bool isGrounded;




    private void Start()
    {
        rb1 = GetComponent<Rigidbody>();

       
    }

    void OnTriggerEnter(Collider Collision)
    {
        if (Collision.gameObject.tag == "Base_Enemy")
        {
            Debug.Log("Player Hit");
            Rigidbody rb1 = Collision.GetComponent<Rigidbody>();

            if (rb1 != null)
            {
                velocity = rb1.transform.position - Collision.transform.position;
                moveDirectionPush = Collision.transform.position - rb1.transform.position;
                rb1.AddForce(velocity.normalized * -500f);

            }


        }
    }


    // Update is called once per frame
    void Update()
    {
         float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");

        if (Input.GetButtonDown("Jump") && isGrounded)
        {
            velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
        }


        Vector3 move = transform.right * x + transform.forward * z;
        controller.Move(move * speed * Time.deltaTime);
        velocity.y += gravity * Time.deltaTime;
        controller.Move(velocity * Time.deltaTime);

        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

        if (isGrounded && velocity.y < 0)
        {
            velocity.y = -2f;
        }


    }
        
}```
#

This is the code and I based it off this

#

But it still doesnt work

pliant pelican
#

Do not use Character controller with a rigidbody
Character controller has its own implementation of rigidbodies

If you use them both together, youre very likely to end up with a lot of issues

In this case i'd highly suggest doing all movement with rigidbodies alone

little compass
#

Alright

#
// Update is called once per frame
    void Update()
    {
         float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");

        if (Input.GetButtonDown("Jump") && isGrounded)
        {
            velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
        }

        Vector3 m_input = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));


        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

        if (isGrounded && velocity.y < 0)
        {
            velocity.y = -2f;
        }


    }
        
}
#

This should have changed it

#

Na, killed the movement

#

Give me a bit of time

little compass
#

@pliant pelican How do I do movement with rigidbodies?

pliant pelican
#

google