Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by marlavdm · Jul 13, 2018 at 07:20 PM · movementmovement scriptcharacter movementunderwatermovements

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;
     }

 }}
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image marlavdm · Jul 14, 2018 at 10:51 AM 0
Share

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 down arrow key

![When I press the right arrow key][3]

down-arrow-key.jpg (20.4 kB)
right-arrow-key.jpg (24.1 kB)
avatar image imM4TT marlavdm · Jul 14, 2018 at 05:07 PM 0
Share

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

avatar image marlavdm imM4TT · Jul 14, 2018 at 06:28 PM 0
Share

I got it, I had to change the Z scale from 1 to -1.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

137 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What should i write that when i press "Shift" it will speed up idk how shift is named? Here's the code: 1 Answer

Boundaries for Camera Movement 1 Answer

How do I make a 3d character only walk backwards? 1 Answer

Walljump 2D doesn't work why ? 1 Answer

How to code a burst of speed? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges