3d movement in Multiplayer?

hello all,

i have a problem with multiplayer movement for my game.

i do have this code:

var MoveSpeed = 0.1;
var TurnSpeed = 0.1;
var JumpHeigh = 1.0;

function OnGUI() 
{
	if (Input.GetKey (KeyCode.W))
	{
		GameObject.Find("Player(Clone)").transform.position.z += MoveSpeed;
	} 
	
	if (Input.GetKey (KeyCode.S))
	{
		GameObject.Find("Player(Clone)").transform.position.z += -MoveSpeed;
	} 
	
	if (Input.GetKey (KeyCode.A))
	{
		GameObject.Find("Player(Clone)").transform.rotation.y += -TurnSpeed;
	} 
	
	if (Input.GetKey (KeyCode.D))
	{
		GameObject.Find("Player(Clone)").transform.rotation.y += TurnSpeed;
	} 
	
	if (Input.GetKeyDown (KeyCode.Space))
	{
		GameObject.Find("Player(Clone)").transform.position.y += JumpHeigh;
	} 
}

but if i press D then is my player rotating, but if he is 30 grades rotated end i press w or s then he moven not in the direction.

i hope you can help my.

===========================================
Sorry for my not so verry good englisch i am dutch
===========================================

I want to know why you are doing this in an OnGui function. I normally handle input with an update function, but maybe I’m just not doing things right.

Kage, I think you are doing it right.=/

No, not realy. it should be called in an update function and it would in general be better if you used a characterController.
Also try to assign the object to move once, avoiding ‘GameObject.Find(“Player(Clone)”’ each frame.

void FixedUpdate()
{
	CharacterController controller = (CharacterController)GetComponent(typeof(CharacterController));
	transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
		if (controller.isGrounded)
		{
			moveDirection = new Vector3(0, 0,Input.GetAxis("Vertical"));
			moveDirection = transform.TransformDirection(moveDirection);
			moveDirection *= speed;

			if (Input.GetButton ("Jump"))
			{
				moveDirection.y = jumpSpeed;
			}
		}

		moveDirection.y -= gravity * Time.deltaTime;
		controller.Move(moveDirection * Time.deltaTime);
}

something along the lines of this.

oke, thx for the fast replies, but i need the code:GameObject.Find("Player(Clone)for find your player in the multiplayer game.

do you realy need to find a gameobject every frame ? there are different ways to handle that, create a start function and search there for the target and use the variable in your update function. the start function is only executed once. searching for the same GO every frame is realy not the way to go.

should i do something look this:

function .... ()
{
   with (GameObject.Find("Player(Clone))
   {
      here the actions
   }
}

Appels, thank you for the humorous reply. Kage said, “I normally handle input with an update function” which I said I supported as well, then you point out " it should be called in an update function". If I were talking to Jaapi, I would have started my response with “Jaapi, …”. =/

oops sorry mate, i missread that one :slight_smile:

jaapi :

var target : Transform;

function Start() {
     target = GameObject.Find("mytarget").transform;
}

No biggie, Appels. I was just a little baffled for a bit and decided to give you a hard time about it.