(スクリプト)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharaBotX : MonoBehaviour
{
public CharacterController controller;
public float speed;
private Vector3 moveDirection = Vector3.zero;
private float gravity = 9.8f;
private Rigidbody rb;
private void Start()
{
rb = GetComponent<Rigidbody>();
rb.isKinematic = true;
}
void Update()
{
moveDirection.z = Input.GetAxis("Vertical");
transform.Rotate(0, Input.GetAxis("Horizontal"), 0);
if (moveDirection.magnitude > 0.1f)
{
Vector3 globalDirection = transform.TransformDirection(moveDirection);
controller.Move(globalDirection * speed * Time.deltaTime);
}
moveDirection.y -= gravity * Time.deltaTime;
if (controller.isGrounded)
{
moveDirection.y = 0;
}
}
private void OnTriggerEnter(Collider other)
{
if(other.CompareTag("Accel"))
{
controller.enabled = false;
rb.isKinematic = false;
// ★真後ろに加速させる(ポイント)
rb.AddForce(-transform.forward * 10, ForceMode.VelocityChange);
}
}
}
(実行結果)