Reversing controls

Easy Question, i have a basic setup that i have taken from the platformer tutorial, i’d like to make it so that when my player picks up an item (negative item) the controls become reverse as in up will move you backwards, left will move right etc.

I’m not really sure the easiest way to do this and was wondering if it had to do with just changing the forward vector or something like that?

Thanks alot!

It depends how you are controlling your player object. But eventually it will come down to something like replacing (or with an if statement to check which one is needed)

Input.GetAxis("Horizontal")

with

-Input.GetAxis("Horizontal")

which is pretty straight forward :slight_smile:

Hey thanks for the reply, i tried that and it didn’t effect the controls. The code is the same as the platform tutorial i.e:

function UpdateSmoothedMovementDirection ()
{
	var cameraTransform = Camera.main.transform;
	var grounded = IsGrounded();
	
	// Forward vector relative to the camera along the x-z plane	
	var forward = cameraTransform.TransformDirection(Vector3.forward);
	forward.y = 0;
	forward = forward.normalized;

	// Right vector relative to the camera
	// Always orthogonal to the forward vector
	var right = Vector3(forward.z, 0, -forward.x);

	var v = Input.GetAxisRaw("Vertical");
	var h = Input.GetAxisRaw("Horizontal");

	// Are we moving backwards or looking backwards
	if (v < -0.2)
		movingBack = true;
	else
		movingBack = false;
	
	var wasMoving = isMoving;
	isMoving = Mathf.Abs (h) > 0.1 || Mathf.Abs (v) > 0.1;
		
	// Target direction relative to the camera
	var targetDirection = h * right + v * forward;
	
	// Grounded controls
	if (grounded)
	{
		// Lock camera for short period when transitioning moving  standing still
		lockCameraTimer += Time.deltaTime;
		if (isMoving != wasMoving)
			lockCameraTimer = 0.0;

		// We store speed and direction seperately,
		// so that when the character stands still we still have a valid forward direction
		// moveDirection is always normalized, and we only update it if there is user input.
		if (targetDirection != Vector3.zero)
		{
			// If we are really slow, just snap to the target direction
			if (moveSpeed < walkSpeed * 0.9  grounded)
			{
				moveDirection = targetDirection.normalized;
			}
			// Otherwise smoothly turn towards it
			else
			{
				moveDirection = Vector3.RotateTowards(moveDirection, targetDirection, rotateSpeed * Mathf.Deg2Rad * Time.deltaTime, 1000);
				
				moveDirection = moveDirection.normalized;
			}
		}
		
		// Smooth the speed based on the current target direction
		var curSmooth = speedSmoothing * Time.deltaTime;
		
		// Choose target speed
		//* We want to support analog input but make sure you cant walk faster diagonally than just forward or sideways
		var targetSpeed = Mathf.Min(targetDirection.magnitude, 1.0);
	
		// Pick speed modifier
		if (Input.GetButton ("Fire3"))
		{
			targetSpeed *= runSpeed;
		}
		else if (Time.time - trotAfterSeconds > walkTimeStart)
		{
			targetSpeed *= trotSpeed;
		}
		else
		{
			targetSpeed *= walkSpeed;
		}
		
		moveSpeed = Mathf.Lerp(moveSpeed, targetSpeed, curSmooth);
		
		// Reset walk time start when we slow down
		if (moveSpeed < walkSpeed * 0.3)
			walkTimeStart = Time.time;
	}
	// In air controls
	else
	{
		// Lock camera while in air
		if (jumping)
			lockCameraTimer = 0.0;

		if (isMoving)
			inAirVelocity += targetDirection.normalized * Time.deltaTime * inAirControlAcceleration;
	}
	

		
}

Anyone have any ideas? I could just reverse everything in that function but that seems like a terrible way to do it i was thinking it would have been something easy?

Code is hard(er) to look at when you’ve not written it yourself.

I think what you are looking for is the variable moveDirection. Try:

moveDirection = moveDirection.normalized;

into

moveDirection = -moveDirection.normalized;

Remember that there are 2 of them!

Nah still no luck i could only find it once in that function as well.

hmm… This function doesnt actually move your object. Do you know where to find that? (e.g. transform.position or Translate(), rididbody.AddForce() or CharacterController.Move() being used?)

Here is the entire script it is from the platforming tutorial as i am new to unity i thought it would be a good idea to use it as a base.

Thanks so much for all your help so far!

70743–2651–$thirdpersoncontroller_109.js (12.7 KB)

Try replacing

// Calculate actual motion
	var movement = moveDirection * moveSpeed + Vector3 (0, verticalSpeed, 0) + inAirVelocity;
	movement *= Time.deltaTime;

with

// Calculate actual motion
	var movement = -moveDirection * moveSpeed + Vector3 (0, verticalSpeed, 0) + inAirVelocity;
	movement *= Time.deltaTime;

OR try replacing

collisionFlags = controller.Move(movement);

with

collisionFlags = controller.Move(-movement);

I’m totally guessing here…

Ok the first one kind of works but it has a weird effect the walking direction seems correct but it jitters all over the place, as in the character faces the right way then the wrong way really quickly while walking. It doesn’t always happen though.

EDIT: Got it all sorted out thanks alot Adriaan!