I don't understand the CharacterMotor script

Can please someone explain me this CharacterMotor script. I’m creating this game and I want my character to have enemies. All I need is the script to go forward. If anybody knows an easyer way to create moving, please say. I’ve tryed to add force to my character but I have trouble with stopping.

Thank you.

CharacterMotor.js contains many comments explaining how it works, for example:

// How high do we jump when pressing jump and letting go immediately
var baseHeight : float = 1.0;
	
// We add extraHeight units (meters) on top when holding the button down longer while jumping
var extraHeight : float = 4.1;

Are there some particular lines that you don’t understand?

I would also point out that you can use CharacterMotor.js without understanding how it works under the covers, just as you can drive a car without understanding how the automatic transmission functions. As an example, here’s how FPSInptuController.js, part of the FirstPersonController prefab, uses it to achieve movement:

function Awake () {
	motor = GetComponent(CharacterMotor);
}

function Update() {
...
	// Apply the direction to the CharacterMotor
	motor.inputMoveDirection = transform.rotation * directionVector;
	motor.inputJump = Input.GetButton("Jump");
}

That’s a much smaller and simpler script and may be all you need to figure out. :slight_smile:

I see you understand the code. Can you please just copy the part which I will copy to my move.js which I’ll apply to the enemy. I’ll make my (enemy) character has a code to rotate itself too look at my character and (if it’s further than, lets say, 20 units) go towards me. Can you please copy all the elements I need to go forward and paste it here so I can copy it from here and paste it in my “if” function where I check the distance. Thank you.

oh, I haven’t seen your response. I was starting to type my before you psted. Thank you very much. It’ll help me a lot. BTW,
when you typed motor.inputMoveDirection = transform.rotation * directionVector; ,
can I write

var someObject: Transform

motor.inputMoveDirection = someObject.rotation * directionVector;

Will this code move my character to someObject?

Ahh, okay! :slight_smile:

If you’re not attaching this to your enemy, var someObject : transform, which you point to the enemy in the inspector, would be a good approach. If you are attaching this to your enemy, transform.rotiation is simpler.

You’ll also need to define directionVector. FirstPersonController.js uses the keyboard/controller input to decide the direction the player will face and the speed at which they move:

// Get the input vector from kayboard or analog stick
var directionVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));

You’ll probably be deciding your enemy’s direction and speed another way.

I will explain my plan to you:

I make a character in 3ds Max. I imported it in the Unity. I put the First Person Controller to the scene. Instead of the capsule, I put my character in the First Person Controller. I can now control my character.
The problem is, I don’t know how to move my enemy. This is the reason I started this. I tough I will make it something like this:
I check the distance between me and my enemy under the Update function. Under the same function, I check the angle between the enemy and me and I rotate the enemy with transform.rotate function. All I need is the functon that will make my enemy walk straight forward. Nothing else.

I’ve been looking at this script for a while now and I don’t understang it at all.

Remember, I want my object (enemy) to go forward. I can do the rotation.
Thank you very much.
I’m making this game for some kind of a software competition, so, if you would like to, I can show them your site or something while I’ll be demonstrating my game…

You should have a look at the Tutorials on this site, and make sure that you understand every bit of it.

In contrast to the previous poster, I think you need to understand how a car functions to be able to repair and tune the car to your needs.

Ergo, without understanding the basics of scripting, you will not be able to script even if your life depends on it.

Method Nr 2:
If your good with art, post in the collaboration section that you are looking for a programmer.