Object rotation

So Im currently trying building a 2.5D Online Multiplayer Game kinda like SuperSmash. Im using the Unity Multiplayer:Intermediate corse to do so. But I ran into a bit of a snag. I am successfully able to Instantiate a projectile on the side my player is facing. However, the projectile itself does not rotate and does not move like its supposed unless i hardcode a number into either the vector2 or vector3. Here is my code if anyone has any suggestions that would be great.

[Command] private void CmdSpawnProjectile()
{
GameObject projectile = Instantiate(projectilePrefab, muzzle.position, Quaternion.identity);
projectile.GetComponent().velocity = new
Vector2(muzzle.transform.rotation.x*projectileSpeed,0);
// this will not allow my projectile to move at all or rotate.
// it works if i just do vector2(20,0) but still doesn’t rotate or move the correct direction.
}

What’s the value of projectileSpeed? From what you’re saying it sounds like it’s 0.

Also, you can’t really use muzzle.transform.rotation.x the way you did. rotation is a Quaternion and the x is probably not quite what you think it is. In fact, this may actually be the problem. You would rather use muzzle.transform.forward but it depends on the orientation of the muzzle.

5A395EC4-ED9C-44A2-AD03-1B4D00C8A613

Apologies for the bad quality. I had to convert a video to a gif in order to upload and show what Im making. Im not sure how to use muzzle.transform.forward. Below is what I currently have. Im able to move the projectile but not in the right direction. Also having issues getting it to show up on my client. It only shows on host.

    [Server] private void CmdSpawnProjectile()
    {
        GameObject projectile = Instantiate(projectilePrefab, muzzle.position, Quaternion.identity);
        projectile.GetComponent<Rigidbody>().velocity = new Vector2(projectileSpeed,0);
    }

It spawns when I turn left. It just gets destroyed as soon as it moves right and hits the owner.

ignore the [Server] I changed it back to [Command]

I can’t see your transform gizmo but it looks like it would rather be transform.right

the transform directions are what the orientation is in relation to the axis. Your code will probably be something like

[Server] private void CmdSpawnProjectile()
{
    GameObject projectile = Instantiate(projectilePrefab, muzzle.position, Quaternion.identity);
    projectile.GetComponent<Rigidbody>().velocity = muzzle.transform.right * projectileSpeed;
}

That is if the muzzle is pointing along the x-axis, which it looks like it is.

I don’t have much knowledge on the multiplayer stuff, but you probably don’t want to use Instantiate but rather the network stuff. I can’t remember what it is. Something like NetworkSpawn

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms