53 lines
1.3 KiB
C#
53 lines
1.3 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class FishController : MonoBehaviour
|
|
{
|
|
public float speed = 4f;
|
|
public float gravity = -9.81f;
|
|
public Vector3 velocity;
|
|
public Vector3 drag;
|
|
|
|
CharacterController controller;
|
|
Transform t;
|
|
|
|
void Start()
|
|
{
|
|
t = transform;
|
|
controller = GetComponent<CharacterController>();
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
float moveX = Input.GetAxisRaw("Horizontal");
|
|
float moveZ = Input.GetAxisRaw("Vertical");
|
|
float moveGravity = gravity * Time.deltaTime;
|
|
|
|
if (controller.isGrounded && velocity.y < 0)
|
|
{
|
|
velocity.y = 0;
|
|
}
|
|
|
|
if (Input.GetButtonDown("Jump") && controller.isGrounded)
|
|
{
|
|
velocity.y += 8;
|
|
}
|
|
|
|
velocity.x = moveX * speed;
|
|
velocity.z = moveZ * speed;
|
|
velocity.y += moveGravity;
|
|
|
|
Vector3 moveVelocity = velocity * Time.deltaTime;
|
|
|
|
if (moveVelocity.y < 0 && Math.Abs(moveVelocity.y) < controller.minMoveDistance)
|
|
{
|
|
Debug.Log(String.Format("Abs velocity: {0}, controller.minMoveDistance: {1}", Math.Abs(moveVelocity.y), controller.minMoveDistance));
|
|
moveVelocity.y = (gravity * 0.1f) * Time.deltaTime;
|
|
}
|
|
|
|
controller.Move(moveVelocity);
|
|
}
|
|
}
|