enemy with character controller code example
Example 1: unity 3d character controller
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour
{
CharacterController characterController;
public float speed = 6.0f;
public float jumpSpeed = 8.0f;
public float gravity = 20.0f;
private Vector3 moveDirection = Vector3.zero;
void Start()
{
characterController = GetComponent<CharacterController>();
}
void Update()
{
if (characterController.isGrounded)
{
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
moveDirection *= speed;
if (Input.GetButton("Jump"))
{
moveDirection.y = jumpSpeed;
}
}
moveDirection.y -= gravity * Time.deltaTime;
characterController.Move(moveDirection * Time.deltaTime);
}
}
Example 2: unity character controller moving platform
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SC_MovingPlatform : MonoBehaviour
{
public Transform activePlatform;
CharacterController controller;
Vector3 moveDirection;
Vector3 activeGlobalPlatformPoint;
Vector3 activeLocalPlatformPoint;
Quaternion activeGlobalPlatformRotation;
Quaternion activeLocalPlatformRotation;
void Start()
{
controller = GetComponent<CharacterController>();
}
void Update()
{
if (activePlatform != null)
{
Vector3 newGlobalPlatformPoint = activePlatform.TransformPoint(activeLocalPlatformPoint);
moveDirection = newGlobalPlatformPoint - activeGlobalPlatformPoint;
if (moveDirection.magnitude > 0.01f)
{
controller.Move(moveDirection);
}
if (activePlatform)
{
Quaternion newGlobalPlatformRotation = activePlatform.rotation * activeLocalPlatformRotation;
Quaternion rotationDiff = newGlobalPlatformRotation * Quaternion.Inverse(activeGlobalPlatformRotation);
rotationDiff = Quaternion.FromToRotation(rotationDiff * Vector3.up, Vector3.up) * rotationDiff;
transform.rotation = rotationDiff * transform.rotation;
transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0);
UpdateMovingPlatform();
}
}
else
{
if (moveDirection.magnitude > 0.01f)
{
moveDirection = Vector3.Lerp(moveDirection, Vector3.zero, Time.deltaTime);
controller.Move(moveDirection);
}
}
}
void OnControllerColliderHit(ControllerColliderHit hit)
{
if (hit.moveDirection.y < -0.9 && hit.normal.y > 0.41)
{
if (activePlatform != hit.collider.transform)
{
activePlatform = hit.collider.transform;
UpdateMovingPlatform();
}
}
else
{
activePlatform = null;
}
}
void UpdateMovingPlatform()
{
activeGlobalPlatformPoint = transform.position;
activeLocalPlatformPoint = activePlatform.InverseTransformPoint(transform.position);
activeGlobalPlatformRotation = transform.rotation;
activeLocalPlatformRotation = Quaternion.Inverse(activePlatform.rotation) * transform.rotation;
}
}