Swimming in a sinusoidal fashion, positive cycle out of water and negative within. Am I close?
Hello,
What the script is trying to accomplish isn't your average swimming. I'm intending to make it like dolphins, as in how they crest above the water and back in. Having control of direction with rotation while in water, but no control over forward momentum. Using unity character controller, I partially succeeded by using the water collider to invert gravity. The player will not always be in water so a basic movement has been attached for land walking.
Aims:
to have increased speed underwater
using rotation only to move
having a constant forward motion once entering water
a sinusoidal swim arc and once out of water you lose rotation control of direction (but keep player with camera rotation) - retaining forward momentum
So I nearly had it working with a slight problem of cresting out of the water, the momentum was aimed at global +Z. After tinkering with it, I broke it, didn't save the original. Trying to replicate the event but not quite getting it. I once had to it working so every time you entered the water collider, you pushed under at a fixed distance and emerged for a small fixed distance. Right now, the amplitude of the swim arc is too exaggerated. The below code is where I'm tinkering from.
From my little scripting experience this was the only way I could imaging how to do it, any help would be vastly appreciated.
Top down and Side on example :
using UnityEngine;
using System.Collections;
public class MoveThePlayer : MonoBehaviour {
private float speed = 60.0F;
private float swimSpeed = 1.5F;
private float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
private Vector3 swimDirection = Vector3.zero;
void OnTriggerEnter(Collider other)
{
if (other.gameObject.layer == LayerMask.NameToLayer ("Water"))
gravity = -100F;
}
void OnTriggerStay(Collider other)
{
CharacterController controller = GetComponent<CharacterController>();
if (other.gameObject.layer == LayerMask.NameToLayer ("Water"))
controller.Move(swimDirection * Time.deltaTime);
swimDirection = new Vector3(0, 0, Input.GetAxis("Vertical"));
swimDirection = transform.TransformDirection(moveDirection);
swimDirection *= swimSpeed;
swimDirection.y -= gravity * Time.deltaTime;
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.layer == LayerMask.NameToLayer ("Water"))
gravity = 20F;
}
void FixedUpdate()
{
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded)
{
moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
}
if(Input.GetKey("a"))
{
transform.Rotate( 0,-2,0, Space.World);
}
if (Input.GetKey ("d"))
{
transform.Rotate (0, 2, 0, Space.World);
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}
Your answer
Follow this Question
Related Questions
A graph to render sine wave with custom resolution 0 Answers
Character's jumping mechanism stuck into something invisible 1 Answer
Best way to simplify this code 1 Answer
Game object not enabled after SetActive(true). 1 Answer
[C#]How to save a list of items to use in another script and it's not a player prefs type of list? 0 Answers