Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Miyahonmoto · Sep 28, 2020 at 05:30 AM · movementmovement scriptmove an object

I want to move the object more smooth way

In my game, I am just building a program for obstacle that repeats going backward and forward, but the movement of the obstacle is like a enemies in the space invaders game although I want to make it more smooth way. I think there should be more better way of the object movement, how can I change this code for moving obstacle in more smooth way?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class RepeatMovementOfShark : MonoBehaviour
 {
     public float timer = 0f;
     public float timeToMove = 0.5f;
     public float ZaxisMovement;
     public int NumberOfMoveToBeReadyToGoNext;
 
     int numOfMovements = 0;
 
     // Start is called before the first frame update
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void Update()
     {
         timer += Time.deltaTime;
         if (timer > timeToMove && numOfMovements < NumberOfMoveToBeReadyToGoNext)
         {
             transform.Translate(new Vector3(0, 0, ZaxisMovement * Time.deltaTime));
             timer = 0f;
             numOfMovements++;
         }
         if (numOfMovements == NumberOfMoveToBeReadyToGoNext)
         {
             numOfMovements = -1;
             ZaxisMovement = -ZaxisMovement;
             timer = 0f;
         }
     }
 }
 
Comment
Add comment · Show 1
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 AlbieMorrison · Sep 29, 2020 at 03:02 AM 0
Share

Does your "Shark" have a Rigidbody attached?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by rh_galaxy · Sep 29, 2020 at 05:11 AM

This will give you smooth movement by moving the shark a little every frame. There are many ways to do it, but is should get you started...

 public float zSpeed = 1.0f; //1.0 unit per second
 public float zDistance = 10.0f; //distance to travel before turning back
 float way = 1.0f;
 float zPos = 0.0f;
 void Update()
 {
     zPos += way * zSpeed * Time.deltaTime;
     transform.Translate(new Vector3(0, 0, way * zSpeed * Time.deltaTime));
     if(zPos>zDistance) way = -1.0f;
     if(zPos<0.0f) way = 1.0f;
 }
Comment
Add comment · Show 2 · 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 Miyahonmoto · Sep 30, 2020 at 05:42 AM 0
Share

Thank you for solving my problem, I finally make the movement of these object more smooth way, however I want to edit the direction of the object. This is because the direction of the object is only going forward and backwards. I want to move one object moving backwards then forward and another one is moving forward then backwards, How can I solve this by inspector or code? I'll put the image what I want to do.alt text

going-backwards-and-going-forward.png (194.6 kB)
avatar image rh_galaxy Miyahonmoto · Sep 30, 2020 at 10:57 AM 0
Share

This would be a way to do it, and also to fix so the shark doesn't wander off which could happen in the code above. So you set the shark going one direction to zSpeed -1 and the other to 1. I think this should work but it is untested.

 public float zSpeed = -1.0f; //units per second, set negative to go the other way
 public float zDistance = 10.0f; //distance to travel before turning back
 Vector3 startPos;
 float way = 1.0f;
 float zPos = 0.0f;
 void Start()
 {
     //save start position
     startPos = transform.position;
 }
 void Update()
 {
     //advance position
     zPosPrev = zPos;
     zPos += way * zSpeed * Time.deltaTime;
     //handle turn around at end
     if($$anonymous$$athf.Abs(zPos)>zDistance) {
         way = -way;
         zPos = zPosPrev;
     }
     //handle turn around at start
     if((zPos<0.0f && zPosPrev >0.0f) || (zPosPrev<0.0f && zPos>0.0f)) {
         way = -way;
         zPos = zPosPrev;
     }
     //set new position
     transform.position = new Vector3(transform.position.x, transform.position.y, startPos.z + zPos);
 }

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

205 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 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

Problem with player bouncing or jumping 1 Answer

GameObject won't move with transform.translate or rigidbody2d.moveposition,GameObject won't move in void Start() 1 Answer

How can I make a prefab object from a list to move?,How can I make an object from a list to move by itself? 2 Answers

Slowly move a GameObject on 1 axis, then destroy it. 1 Answer

How can i make diagonal movement? 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