- Home /
Question by
Dragon_Developer · May 02, 2016 at 02:55 PM ·
networkingbugrigidbody2dvectorcalculation
uNet spawning projectiles with calculated direction going wrong way on clients.
I have some code to spawn a rocket, but when spawning it while being a client the rocket goes to a random direction... I think this has to do with the execution order, but I cant seem to fix it.
[Command]
public void CmdFire()
{
GameObject rocket = Instantiate(RocketObject, rocketSpawnLocation.transform.position, Quaternion.identity) as GameObject;
NetworkServer.Spawn(rocket);
Vector2 crosshairLocation = new Vector2(crosshair.transform.position.x, crosshair.transform.position.y);
Vector2 spawnLocation = new Vector2(rocketSpawnLocation.transform.position.x, rocketSpawnLocation.transform.position.y);
Vector2 direction = new Vector2(crosshairLocation.x - rocket.transform.position.x, crosshairLocation.y - rocket.transform.position.y);
direction.Normalize();
direction *= (rocketSpeed * 100);
Quaternion rotation = Quaternion.LookRotation(new Vector3(crosshairLocation.x, crosshairLocation.y) - rocket.transform.position, rocket.transform.TransformDirection(Vector3.up));
rocket.transform.rotation = new Quaternion(0, 0, rotation.z, rotation.w);
for (int i = 0; i <= smokeAmount; i++)
{
spawnSmoke(new Vector2(Random.Range(rocket.transform.position.x - smokeOffset, rocket.transform.position.x + smokeOffset), Random.Range(rocket.transform.position.y - smokeOffset, rocket.transform.position.y + smokeOffset)));
}
rocket.GetComponent<Rigidbody2D>().AddForce(direction);
Destroy(rocket, 2.0f);
}
Comment
Your answer
Follow this Question
Related Questions
Colliding players spin out of control and rotation is local. Need help! [C#] 0 Answers
UNET "no free events for message" when client pauses game. 2 Answers
Threading with UDPClient - Editor Freezing 2 Answers
UNET. Ho to use different NetworkManager in offline and online scane? 0 Answers
Network.Connect Fails To Connect 0 Answers