55 lines
1.3 KiB
C#
55 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 float jumpSpeed = 8;
|
|
public Vector3 velocity;
|
|
public Vector3 drag;
|
|
|
|
CharacterController controller;
|
|
Transform t;
|
|
|
|
void Start()
|
|
{
|
|
t = transform;
|
|
controller = GetComponent<CharacterController>();
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
float moveX = 0 - Input.GetAxisRaw("Vertical");
|
|
float moveZ = Input.GetAxisRaw("Horizontal");
|
|
float moveGravity = gravity * Time.deltaTime;
|
|
|
|
if (controller.isGrounded && velocity.y < 0)
|
|
{
|
|
velocity.y = 0;
|
|
}
|
|
|
|
if (Input.GetButtonDown("Jump") && controller.isGrounded)
|
|
{
|
|
velocity.y += jumpSpeed;
|
|
}
|
|
|
|
velocity.x = moveX * speed;
|
|
// velocity.z = moveZ * speed;
|
|
velocity.y += moveGravity;
|
|
|
|
transform.Rotate(0, moveZ, 0);
|
|
|
|
Vector3 moveVelocity = velocity * Time.deltaTime;
|
|
|
|
if (moveVelocity.y < 0 && Math.Abs(moveVelocity.y) < controller.minMoveDistance)
|
|
{
|
|
moveVelocity.y = (gravity * 0.1f) * Time.deltaTime;
|
|
}
|
|
|
|
controller.Move(transform.rotation * moveVelocity);
|
|
}
|
|
}
|