Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

I don't understand the CharacterMotor script

Discussion in 'Scripting' started by malnar1996, Dec 11, 2010.

  1. malnar1996

    malnar1996

    Joined:
    Oct 31, 2010
    Posts:
    31
    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.
     
  2. Ostagar

    Ostagar

    Joined:
    Sep 29, 2010
    Posts:
    445
    CharacterMotor.js contains many comments explaining how it works, for example:
    Code (csharp):
    1. // How high do we jump when pressing jump and letting go immediately
    2. var baseHeight : float = 1.0;
    3.    
    4. // We add extraHeight units (meters) on top when holding the button down longer while jumping
    5. var extraHeight : float = 4.1;
    Are there some particular lines that you don't understand?
     
  3. Ostagar

    Ostagar

    Joined:
    Sep 29, 2010
    Posts:
    445
    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:

    Code (csharp):
    1.  
    2. function Awake () {
    3.     motor = GetComponent(CharacterMotor);
    4. }
    5.  
    6. function Update() {
    7. ...
    8.     // Apply the direction to the CharacterMotor
    9.     motor.inputMoveDirection = transform.rotation * directionVector;
    10.     motor.inputJump = Input.GetButton("Jump");
    11. }
    That's a much smaller and simpler script and may be all you need to figure out. :)
     
    Last edited: Dec 11, 2010
  4. malnar1996

    malnar1996

    Joined:
    Oct 31, 2010
    Posts:
    31
    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.
     
  5. malnar1996

    malnar1996

    Joined:
    Oct 31, 2010
    Posts:
    31
    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?
     
  6. Ostagar

    Ostagar

    Joined:
    Sep 29, 2010
    Posts:
    445
    Ahh, okay! :)

    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:

    Code (csharp):
    1. // Get the input vector from kayboard or analog stick
    2. var directionVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    You'll probably be deciding your enemy's direction and speed another way.
     
    Last edited: Dec 11, 2010
  7. malnar1996

    malnar1996

    Joined:
    Oct 31, 2010
    Posts:
    31
    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...
     
  8. hallamasch

    hallamasch

    Joined:
    Nov 29, 2010
    Posts:
    153
    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.