The question is answered, right answer was accepted
Player object can, but should not, climb walls, slingshot themselves, and given enough time, can run through the ground.
So I apologize in advance, as I am relatively new to several of these concepts, and I feel like a moron already. Additionally, I have searched the forums and google and I have yet to find an answer to this problem, but more than likely I just do not know how to properly convey what is occurring in such a manner as to have search engines return usable results.
I have a player game object with a capsule collider. If I position the player object (capsule) and rotate the camera to be looking upward at another game object with a collider, we'll use a box collider on a wall, but it happens on mesh colliders as well, it is allowing the player object to climb up the wall.
In addition, if I happen to run into that box collider, and maintain a forward heading, then turn slightly to slide off the collider, once I am clear of the collision, I get a boost of forward and upward velocity....Lastly, if I angle the camera downward, the player object will begin to work itself through the ground object.
Now I have tried continuous detection of collision on the Rigidbody, as well as increasing Mass and Drag values in order to minimize the upward and forward velocity. While the latter options minimize the issue of launching the player object, I would like to truly solve the issue, and if possible understand where I messed up or what I missed to cause this.
Thank you so much for your attention and assistance if you have a moment to spare it.
TL/DR: I messed up or missed something on Rigidbody/Collider/Code causing player object to inadvertently climb walls, tunnel through ground colliders, and build up momentum and rocket themselves when sliding off colliders.
Code: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerController : MonoBehaviour { public GameObject cam; public Animator anim; public float speed; public float mspeed;
Rigidbody rb;
CapsuleCollider capsule;
Quaternion cameraRot;
Quaternion characterRot;
bool cursorLocked = true;
bool lockCursor = true;
float x;
float z;
// Start is called before the first frame update
void Start()
{
rb = this.GetComponent<Rigidbody>();
capsule = this.GetComponent<CapsuleCollider>();
cameraRot = cam.transform.localRotation;
characterRot = this.transform.localRotation;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && IsGrounded())
rb.AddForce(0, 375, 0);
}
void FixedUpdate()
{
float yRot = Input.GetAxis("Mouse X") * mspeed;
float xRot = Input.GetAxis("Mouse Y") * mspeed;
cameraRot *= Quaternion.Euler(-xRot, 0, 0);
characterRot *= Quaternion.Euler(0, yRot, 0);
this.transform.localRotation = characterRot;
cam.transform.localRotation = cameraRot;
cameraRot = ClampRotationAxis(cameraRot);
x = Input.GetAxis("Horizontal") * speed;
z = Input.GetAxis("Vertical") * speed;
transform.position += cam.transform.forward * z + cam.transform.right * x; //new Vector3(x * speed, 0, z * speed);
Quaternion ClampRotationAxis(Quaternion q)
{
q.x /= q.w;
q.y /= q.w;
q.z /= q.w;
q.w = 1.0f;
float anglex = 2.0f * Mathf.Rad2Deg * Mathf.Atan(q.x);
anglex = Mathf.Clamp(anglex, -30f, 15f);
q.x = Mathf.Tan(0.5f * Mathf.Deg2Rad * anglex);
return q;
}
UpdateCursorLock();
}
bool IsGrounded()
{
RaycastHit hitInfo;
if (Physics.SphereCast(transform.position, capsule.radius, Vector3.down, out hitInfo,
(capsule.height / 2f) - capsule.radius + 0.1f))
{
return true;
}
return false;
}
public void SetCursorLock(bool value)
{
lockCursor = value;
if (!lockCursor)
{
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
}
public void UpdateCursorLock()
{
if (lockCursor)
InternalLockUpdate();
}
public void InternalLockUpdate()
{
if (Input.GetKeyUp(KeyCode.Escape))
cursorLocked = false;
else if (Input.GetMouseButtonUp(0))
cursorLocked = true;
if (cursorLocked)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
else if (!cursorLocked)
{
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
}
}
Settings:
Answer by Amucity · Sep 15, 2020 at 04:41 AM
Problem resolved.
The problem line of code was:
transform.position += cam.transform.forward * z + cam.transform.right * x;
should have been adjusted to:
transform.position += this.transform.forward * z + cam.transform.right * x;
Reason being, the forward motion was being applied to camera direction, vs. the direction of the GameObject (which the camera was a child of)