Files
fish-escape/Assets/Scripts/FishController.cs
2022-04-11 19:02:46 +01:00

53 lines
1.2 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 = 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 += jumpSpeed;
}
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)
{
moveVelocity.y = (gravity * 0.1f) * Time.deltaTime;
}
controller.Move(moveVelocity);
}
}