- Home /
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;
}
}
}
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;
}
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.
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);
}