- Home /
Question by
alireza97 · Sep 15, 2019 at 02:58 PM ·
transform.positiontransform.rotation
How to Move and Rotate Ball at the same time by code
Hi.
I'm creating a multiplayer Football Game like Soccer Stars.
when 'a' shot, I send the ball position every 200 milliseconds and the other player get it and try to replay movements of ball and units. here is a sample code for this:
Movent Class:
public class Movements
{
public float time;
public Vector3 position;
public Movement_From_Server(Vector3 pos, float t)
{
time = t;
position = pos;
}
}
and GameObjects function:
List<Movements> Positions = new List<Movements>();
int index = 0;
void Replay_Movements()
{
if (index < Positions.Count)
{
movement_timer += Time.deltaTime;
float _deltaT = Positions[index].time - Positions[index - 1].time;
if (_deltaT > 0)
{
float _deltaX = (Positions[index].position.x - Positions[index - 1].position.x) / _deltaT;
float _deltaZ = (Positions[index].position.z - Positions[index - 1].position.z) / _deltaT;
transform.position = new Vector3(Positions[index - 1].position.x + (_deltaX * movement_timer), Positions[index - 1].position.y, Positions[index - 1].position.z + (_deltaZ * movement_timer));
}
if (movement_timer >= _deltaT)
{
movement_timer = 0;
transform.position = Positions[index].position;
index++;
}
}
}
this works for units but when I add it for the ball doesn't work.
I think it might be because of that my unit's rotation of x and z is freeze but the ball rotation has no freeze.
(and I don't know how exactly replay rotate the ball to make it the same as the other player) any idea?
Comment