Reputation: 1
I'm putting together a little 3rd person game demo in Unity with no player movement on the y axis. No jumping, no climbing, nothing. No hills. No valleys. No stairs. Nothing.
But there IS a small pile of wood that the player controller keeps climbing up on. I had it there just for looks but it's proven a frustrating challenge. I'm trying to figure out a way to prevent the character controller from being able to ascend/descend at all on the Y axis.
Here's the code I have set up for player movement, it's ripped straight from Brackeys' first person movement video.
public class PlayerMovementScript : MonoBehaviour
{
public CharacterController controller;
//here a function of unity called CharacterController was used.
//it created another "reference" in unity that a game object can be assigned to
//this is used below
public float speed = 5f;
public float strafe = 1f;
// Update is called once per frame
void Update()
{
float x = Input.GetAxis("Horizontal") * strafe;
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
}
}
I've tried a few different solutions, none have worked. I've attached a rigid body to lock the y position, then a configurable joint. I've tried a few different bits of code to try to update the object's position to y=1 each frame. The settings that come with the character controller component seem to have no bearing on reality. I have both the slope limit and step offset set to 0 and for some reason my character can still climb VERY steep slopes (like 140 degrees) and take pretty large steps. I figured maybe things were getting wedged under the rounded edges on the bottom of the capsule and allowing it to be pushed up, so I've tried to attach a mesh collider of a cylinder to the player to eliminate the small space under the capsule. Player kept climbing anyway.
I'm pretty new to coding, as in like 2 months maybe, and I still don't have this whole coding thing anywhere near understood. But, I feel like this shouldn't be this complicated.
Am I missing something? How can I prevent the character controller from moving up or down? Do I need to just switch to a rigid body and get it over with?
Upvotes: 0
Views: 2747
Reputation: 24
If you have the pile of wood just for looks the easy solution is to disable the collider on the it. But then it won't collide with anything else in the scene. The other solution is that you can assign the obstacles you don't want the player to collide with to a layer like "Obstacle" and the player to another layer such as "Player". Then you can disable collisions between the Player layer and the Obstacle layer in Project settings -> Physics. Click here to see
Upvotes: 1