|
|
|
|  Simpler NPC (unity3d) | | | |
 ![Einen Kommentar (einer Stelle[markiert]) hinzufügen](imgs/iconaddobject.gif)
|  NPC mit der Struktur - XYZ (Leeres GameObject + CharacterControll Comp.) -- 3D-Objekt/Animationsobject |  | | 0. Unterlage erzeugen, wo der NPC laufen kann
 | | | | | 1. Create Empty Object
 | | | | | 2. Umbenennen etwa NPC
 | | | | | 3. Generiere ein Mesh oder nimm ein Primitives-Objekt
 | | | | | 4. Ordne es dem NPC unter
 | | | | | 5. Kreiere ein neues JavaScript-Asset
 | | | | |  6. Benutze folgenden Code Abwandlung von /Assets/Scrips/FPSWalker.js |  | |  NPC.js /* NPC Simple runs in one direction */
var speed = 6.0; var jumpSpeed = 8.0; var gravity = 20.0;
private var moveDirection = Vector3.zero; private var grounded : boolean = false;
function OnControllerColliderHit (hit : ControllerColliderHit) { // CharacterController.OnControllerColliderHit.html /* controller The controller that hit the collider collider The collider that was hit by the controller rigidbody The rigidbody that was hit by the controller. gameObject The game object that was hit by the controller. transform The transform that was hit by the controller. point The impact point in world space normal The normal of the surface we collided with in world space moveDirection Approximately the direction from the center of the capsule to the point we touch. moveLength */ // hit? // print(hit); // hit from which direction? // print(hit.moveDirection); // hitted object // print(hit.gameObject.name);
}
function FixedUpdate() { if (grounded) { // move to (1,0,0) moveDirection=new Vector3(0.2,0,0); moveDirection = transform.TransformDirection(moveDirection); moveDirection *= speed;
// jump // moveDirection.y = jumpSpeed; }
// Apply gravity moveDirection.y -= gravity * Time.deltaTime; // Move the controller var controller : CharacterController = GetComponent(CharacterController); var flags = controller.Move(moveDirection * Time.deltaTime); grounded = (flags & CollisionFlags.CollidedBelow) != 0; }
// onClick function OnMouseDown() { // do something on click of it }
@script RequireComponent(CharacterController) | | | |
| | | | 7. Wende das NPC Script an
 | | | | | 8. NPC Struktur
 | | | | | 9. NPC im Editor mit Collisionsbox (falls das Objekt durch den Boden fällt -> höher setzen)
 | | | |
| | |
|  10. Avatar geht nach rechts, wenn er am Boden ist // move to (1,0,0) moveDirection=new Vector3(0.2,0,0); moveDirection = transform.TransformDirection(moveDirection); moveDirection *= speed; |  | | Avatar Position 0
 | | | | | Avatar Position 1
 | | | |
| | |
|
|
|