Custom movement script problems

Hi there. I am having some issues and have been looking through all the different answers/questions on the site, but haven’t really found anything that hits the nail on the head for my problem.

I am pretty new to Unity and games programming (I know how to code, just not 3D games!). I was wanting to make my own custom movement script. I wanted to make a movement script where the L and R keys rotated the character left and right, the U key moves forward and D key moves backwards, with space for jump. I was also looking for the mouse look to only work when I hold down the right mouse key.

I had been looking at all kinds of examples, but what I notice is, using the standard assets FPSController, when I try to make my own scripts it is essentially overwritten by the generated scripts. When I disable them (the generated scripts), mines don’t do anything. What do I need to do in order to allow the character to move using only my script? Must it have a motor attached to it?

If anyone can help me with the scripts and attaching them properly, I would be very grateful. I know the information is out there, but there is so much information it is a little overwhelming, a little help in narrowing it down would be appreciated.

1 Answer

1

The character controller script is meant to be driven my a second script. To get you started, use Component/Physics/Character Controller to attach a character controller. Then go to the reference for CharacterController.Move() or CharacterController.SimpleMove() and either of these two scripts to your character controller as well.

Neither script of these two scripts rotate the character using the L and R keys. The character just moves both horizontally and vertically. So you will have that change to make. If you have difficulty making the change, let me know which of the two scripts you are using, and I’ll be glad to show you how to make the change.

Hi mate, thanks for getting back. Apologies for the follow up answer instead of comment. I will have a jump feature in, I was just focusing on ground movement at the moment. I shall implement the changes and get back, thanks!

Just to clarify a few things, the ground() method was brought in because when I tried to have a constant gravity effect, the forward/backward movement was also being called, I don't know if my solution is the best, but it manages to work. In my jump code I set the movement vectors to 0, this is because the jump was not only jumping, but moving in the direction it is facing also, I just wanted it to jump when the jump key is pressed.

The code is fine as long as you are happy with it. A minor nit: by convention function and class names start with capital letters so your function should be Ground(). The compiler won't complain, but folks on this list who read your code will have to work harder. If this question is answered, you can you click the checkmark next to the answer to close it out.

Oops my bad, forgot to put caps on it. Could anyone offer any suggestions for making it so that it doesn't glide if I jump then press up or down?

Also, I was wondering how to jump whilst moving as well. Right now it just sort of blips. Having trouble figuring it out. Any help would be appreciated.