- Home /
How to make fish movement with keyboard arrows look natural?
I have a 3D fish in my underwater scene that I want to move using the keyboard arrows.
The code that I have however doesn't make the fish movement look natural, especially when travelling up and down. When I hold in the up button, the entire fish lifts up which looks very rigid - I want the fish movement to look natural by moving diagonally up until I let go of the up button (and the same for the down button). If for example, the fish is facing right and its moving right from, when I change the direction to move left through the left arrow button, I also need the fish to rotate and face the other way. I need the fish movement to look very natural and normal basically :). I have animation that makes the fins flap.
Any ideas how I can achieve this? I'm very new to Unity.
The code I have so far:
public class FishController : MonoBehaviour {
float speed = 5.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKey(KeyCode.RightArrow))
{
transform.position += Vector3.right * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.LeftArrow))
{
transform.position += Vector3.left * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.UpArrow))
{
transform.position += Vector3.up * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.DownArrow))
{
transform.position += Vector3.down * speed * Time.deltaTime;
}
}}
Answer by imM4TT · Jul 13, 2018 at 07:44 PM
You can use Vector3.Lerp() or Vector3.MoveTowards() to smoothly move your character
And you can also use Quaternion.LookRotation() to make your character constatly facing the destination relative to his position.
public class FishController : MonoBehaviour
{
private Quaternion qto;
private Vector3 destination;
public float speed;
public float speedToMove;
public float speedToRotate;
// Use this for initialization
void Start()
{
destination = transform.position;
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.RightArrow))
{
destination += Vector3.right * speed * Time.deltaTime; // Set our destination as the desired one
}
if (Input.GetKey(KeyCode.LeftArrow))
{
destination += Vector3.left * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.UpArrow))
{
destination += Vector3.up * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.DownArrow))
{
destination += Vector3.down * speed * Time.deltaTime;
}
Vector3 target = new Vector3(destination.x - transform.position.x, destination.y - transform.position.y, destination.z - transform.position.z); // Create a new vector relatively to our current position
qto = Quaternion.LookRotation(target); // Set our rotation destination
// And there we go to the desired destination using Move&RotateTowards()
transform.position = Vector3.MoveTowards(transform.position, destination, speedToMove * Time.deltaTime);
transform.rotation = Quaternion.RotateTowards(transform.rotation, qto, speedToRotate * Time.deltaTime);
}
}
It should work
Your code is amzing and it works perfectly to move and rotate the fish. The only thing is the fish always looks in the wrong direction; for example: if I hold in the up arrow key, the fish moves upwards, but looks downwards ins$$anonymous$$d of upwards, and if I hold in the right arrow key, the fish moves to the right but looks left. I've attached pictures to see what I mean, do you know how I can fix this?
![When I press the right arrow key][3]
Hi, im' not really sure about what is exactly causing this
Please make sure that the following things are respected :
- Importation of your 3d model is fine https://docs.unity3d.com/$$anonymous$$anual/HOWTO-FixZAxisIsUp.html
- If your 3d model have parents make sure that all parent's rotation (& position) are set to 0,0,0
I got it, I had to change the Z scale from 1 to -1.