- Home /
Player starts moving in one direction nonstop after colliding with any other object
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: https://vimeo.com/276993825?utm_source=email&utm_medium=vimeo-cliptranscode-201504&utm_campaign=28749
Video Password: Unity
Without all the details relative to the issue I can't entirely say what it is but unless you intend on having it move a tad bit after letting go of the buttons (like a velocity system or something?) then I suggest you try only running the code in FixedUpdate() and others involving movement to only run when a movement key is pressed, since in your video I only ever spotted it when you weren't pressing any buttons.
I may be able to use the current info to solve it after closer inspection but it will take time. Try my suggestion and tell me what happens.
Your Rigidbody Physics drag = 0;
Set it to 10 or 100.
Changed drag to 100 and the issue seems to be gone. Would you $$anonymous$$d explaining to me why changing the drag value fixed the issue?
It's basic of Physics.
Drag is the force that hold the object back when it is on the ground. Slow it down
Answer by AshwinLahare · May 11, 2019 at 10:06 AM
I have the same problem. Looks like it's a bug.
A workaround that has worked well for me is to increase the Radius of the Sphere Collider of the Player just up to the point till it encompasses most of the gun (you can leave the tip of the muzzle out and instantiate bullets from there). The obstacle then collides with the bigger Sphere Collider and the Player stays in place however you collide it.
This will however, make your player easier to hit if shot at.
Your answer
Follow this Question
Related Questions
Problems with collision under certain circumstances. 0 Answers
Rigidbody2D with box colliders passing through each other 0 Answers
Weapon System with collide detection (Helps with script pls)!!! 0 Answers
How to access float from other script? 1 Answer
Instantiate 1 object after 2 objects collide. ( C# ) 1 Answer