TopDown Controls - World Movement

Hello,

I’m working on a top down controller for my character. Right now, it rotates towards where the mouse is on the screen, and when you press ‘W’, or UP it will move forward in the direction of the mouse. ‘S’ or DOWN, will move away and so on.

This works fine, but I also want an option so I can change the movement to world directions, what I mean by that, is no matter what rotation the character is at, ‘W’ or UP will always move Up/North/+Z, and the same with all the other directions. For some reason, I really cant seem to get my head around this?

Below is my script for moving the character in local position, with rotation of the mouse:

// ----- Move Character ----- \\
	//move in local position
	if(!allowWorldMovement){
		if(allowRotateStatic){
			//rotate player to face direction arrow
			player.transform.rotation = Quaternion.Slerp(player.transform.rotation, targetRotation, playerRotateSpeed * Time.deltaTime);
		}
		if(Input.GetAxis("Vertical")){
			//rotate player to face direction arrow
			player.transform.rotation = Quaternion.Slerp(player.transform.rotation, targetRotation, playerRotateSpeed * Time.deltaTime);
        
    	    //them move player towards arrow direction
      	  var translation : float = Input.GetAxis("Vertical") * playerMoveSpeed;
     	   translation *= Time.deltaTime;
        
			player.transform.Translate(0,0,translation);
		}
		if(Input.GetAxis("Horizontal")){
		
			var panning : float = Input.GetAxis ("Horizontal") * playerPanSpeed;
			panning *= Time.deltaTime;
		
			player.transform.Translate(panning,0,0);
	
			//pan the character left/right whilst looking at mouse position/direction arrow
			if(allowRotatePan){
				player.transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, playerRotateSpeed * Time.deltaTime);
			}
		}
	}

And then in the world position, I’m not making any progress, my character ‘tries’ to move, but seems like hes stuck, and then just bounces back to his original position?

//move in world space
	else{
		
		var translationW : float = Input.GetAxis ("Vertical") * 10;
		translationW *= Time.deltaTime;
		
		//if(Input.GetAxis("Vertical")){
			player.transform.position = Vector3(0, 0, translationW);
		//}
	}

It’s quite simple. You just use position.x or position.z.

ExampleScript:

if(Input.GetButton("Test"))
gameObject.transform.position.x+=2*Time.deltaTime;

This makes the gameobject move along the worlds x-axis and it does not depend on its angle.

It think for example Vector3(2,0,0) also works just fine.

Hope this helps.