My current issue is that when my player capsule collides with any other object that has a collider it starts to move nonstop in the opposite direction of the collision.
Player Components:
Components of other colliders:
Player Controller Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class PlayerController : MonoBehaviour {
//Public Variables
public Rigidbody myRigidBody;
public Collider myCollider;
public float playerSpeed = 25;
public float playerRotationSpeed;
//public LayerMask whatIsGround;
//public bool isGrounded;
//Private Variables
private float hAxis;
private float vAxis;
private Camera mainCamera;
private void Start()
{
myRigidBody = GetComponent<Rigidbody>();
myRigidBody.constraints = RigidbodyConstraints.FreezeRotationX;
myCollider = GetComponent<Collider>();
//whatIsGround = 1 << 9;
mainCamera = Camera.main;
}
private void Update()
{
hAxis = Input.GetAxisRaw("Horizontal");
vAxis = Input.GetAxisRaw("Vertical");
//GroundCheck();
LookAtMouse();
}
private void FixedUpdate()
{
Vector3 moveInput = new Vector3(hAxis, 0, vAxis).normalized;
myRigidBody.MovePosition(transform.position + moveInput * playerSpeed * Time.deltaTime);
}
//private void GroundCheck()
//{
// Ray groundCheck = new Ray(transform.position, Vector3.down);
// if (Physics.Raycast(groundCheck, 1.71f, whatIsGround, QueryTriggerInteraction.Ignore))
// {
// isGrounded = true;
// }
// else
// {
// isGrounded = false;
// }
//}
private void LookAtMouse()
{
Ray cameraRay = mainCamera.ScreenPointToRay(Input.mousePosition);
Plane groundPlane = new Plane(Vector3.up, Vector3.zero);
float rayDistance;
if (groundPlane.Raycast(cameraRay, out rayDistance))
{
Vector3 pointToLookAt = cameraRay.GetPoint(rayDistance);
transform.LookAt(new Vector3(pointToLookAt.x, transform.position.y, pointToLookAt.z));
//Debug.DrawLine(cameraRay.origin, pointToLookAt, Color.red);
}
}
}
Link to video with example:
Video Password: Unity