Tuesday, May 24, 2016

c# - Spawning a Sphere and trying to move it




I've been trying to build upon a blog post on Gamasutra http://www.gamasutra.com/blogs/DarrelCusey/20130221/187128/Unity_Networking_Sample_Using_One_NetworkView.php In this I would like to spawn a sphere on the server and move it about and when it moves have the movement sent to the clients. On the GUI on the server I just want 3 buttons, spawn sphere, move left and move right. Ive been able to get the sphere to spawn fine, but when I press the button to have it move left or right nothing happens. Have I thought about this too simply? Where have I went wrong or what have I not included?


//***New Code***
if(GUILayout.Button ("Spawn sphere"))
{
Instantiate(spawnedSphere);
Debug.Log("sphere spawned");
}
if(GUILayout.Button ("Move Sphere Left"))
{
spawnedSphere.rigidbody.AddForce(new Vector3(-5000, 0, 0));

Debug.Log("Sphere went left");
}
if(GUILayout.Button ("Move Sphere Right"))
{
spawnedSphere.rigidbody.AddForce(new Vector3(5000, 0, 0));
Debug.Log("Sphere went right");
}

//***End New Code***

Answer




Replacing "Instaniate(spawnedSphere)" on line 3 with "spawnedSphere = GameObject.Instantiate(spawnedSphere) as GameObject;" fixed the problem.


After instantiating spawnedSphere, I didn't change its value. So I was trying to add force to the prefab.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...