#Trying collision
1 messages · Page 1 of 1 (latest)
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
https://answers.unity.com/questions/1370991/how-to-add-knockback-force-based-on-what-rotation.html and tried to fix it with this
Unity is the ultimate game development platform. Use Unity to build high-quality 3D and 2D games, deploy them across mobile, desktop, VR/AR, consoles or the Web, and connect with loyal and enthusiastic players and customers.
But it still doesnt work
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
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
@pliant pelican How do I do movement with rigidbodies?