Character controller walking around tower

Hi, I am having difficulties in using the Character controller’s Move(…) function in a particular case. I am making a side-scrolling platformer where the player character walking left or right actually walks around a fixed pivot point - a tower. It is pretty much as Castelian, an old game you can try in your browser here: Castelian - Play the Nintendo 8-bit (NES) game Castelian online.

The trouble is finding out how to make the character rotate around the tower and not just walk straight.

I started out with this code I found in the Unity docs:

/// This script moves the character controller forward 
/// and sideways based on the arrow keys.
/// It also jumps when pressing space.
/// Make sure to attach a character controller to the same game object.
var speed = 6.0;
var jumpSpeed = 8.0;
var gravity = 20.0;

private var moveDirection = Vector3.zero;

function FixedUpdate() {
    var controller : CharacterController = GetComponent(CharacterController);
    if (controller.isGrounded) {
        // We are grounded, so recalculate
        // move direction directly from axes
        moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
                                Input.GetAxis("Vertical"));
        moveDirection = transform.TransformDirection(moveDirection);
        moveDirection *= speed;
        
        if (Input.GetButton ("Jump")) {
            moveDirection.y = jumpSpeed;
        }
    }

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

I’ll appreciate any tips to fix the character into a “2d curved world”? Thanks!

Seeing as the player character in your example walks around the tower and stays in the same spot on the screen, have you thought about just rotating the tower rather that moving the player? Seems like an easier option to me.

If you would rather walk the character around the tower and the camera with the character, you will need to keep the player a certain distance from the center of the tower at all times, so basically walk in a circle. To do that you need to think of the players horizontal position as an angle rather than a position, and their speed as how many degrees per second they move, rather than how many meters per second they move.

Have a look at this page to get an idea on circle maths. It should be easy enough to formulate a good algorithm to fix your problem.

Murcho, thanks for your help.

At first the idea of rotating the tower seemed like a great one, but then I thought about many other things moving around the tower in different directions (monsters, projectiles, …) so this is not an option.

Just to save some time, does anyone have an algorythm to calculate the direction vector for such circular movement? My math/physics skills are as rusty as rust.

Thank you!

I assume do everything as normal, but in the calculate forward vector you want to add a slight rotation towards the center point of the tower.

The main issue is…the closer to the center of the tower the tighter you need to turn.

So you’ll get soemthing like

forwardVector = forwardVector + (rotationTowardCenter);

Actually thinking about it more. You could have a helper attached to your player that all ways does a call to face the center of the of the tower. So as you move that helper rotates to look at th center of the tower. You could work out the change in rotation per frame and apply that change to your character as he moves forward around the tower.

-P

I had a bit more of a think about this one. What you want to do is have a vector that you rotate around the axis of the tower. So back to where I said the player speed was a rotation, you rotate this vector by the speed based on input, then place the player at the end of this vector.

Here is a good discussion on the theory behind it all. The code isn’t in any Unity language, but the theory behind it is solid.

make simple box in center of tower, to that box add another object - player, and place it in the distance. Next that you need to do is to rotate box object, and your player will rotate around and make a circle walk :wink:

bump. i’ve started working on a solution to this, adapting the technique from the standard mouse orbit script. it works well except that, i suppose because Move() tries to slide the character along colliders, when it comes up against a wall or other side collision the player begins to rotate and slide along the wall instead of stopping. i’ve tried to patch this up by clamping movement once a side collision is detected, but at a good speed the player will still rotate a little bit “into” the wall and it takes a moment of pressing in the opposite direction to get unstuck. the problem is not noticeable if i move the code to FixedUpdate and crank the physics simulation up to 240 fps, but that’s not practical or good practice. i tried using raycasts to detect walls and clamp movement all the time, instead of just when the character controller detected a collision, but the results were no more predictable than they are now.

i’d like to keep using Move() if possible, because i need to retain the ability for the player character to slide effortlessly along slopes, but i cannot have it sliding along the walls even a little bit. if anyone has any suggestions i’d really appreciate it.

here’s what i’ve got so far, sorry it’s a bit messy:

var target : Transform;
var distance = 10.0;

var xSpeed = 250.0;
var ySpeed = 120.0;

var yMinLimit = -20;
var yMaxLimit = 80;

private var x = 0.0;
private var y = 0.0;

private var rotation;

var movement : Vector3;

var characterController : CharacterController;

function Start () {
    var angles = transform.eulerAngles;
    x = angles.y;
    y = angles.x;

	// Make the rigid body not change rotation
   	if (rigidbody)
		rigidbody.freezeRotation = true;
		
		characterController = GetComponent(typeof(CharacterController)) as CharacterController;
}

function IsCollision (direction : Vector3) {
	var hit : RaycastHit;
	var ray = Ray(transform.position + characterController.center, direction);
	Debug.DrawRay (transform.position + characterController.center, direction * 2, Color.blue);
	if (Physics.Raycast(ray, hit, 2)) {
		return true;
	}
	else {return false;}
	}


function Update () {
    if (target) {
		
		
		xInput = Input.GetAxis("Horizontal") * xSpeed * 0.02;

		
		if (characterController.collisionFlags  CollisionFlags.Sides) {
			if (IsCollision(-transform.right)) {
				xInput = Mathf.Clamp(xInput, 0, 100);
			}
			if (IsCollision(transform.right)) {
				xInput = Mathf.Clamp(xInput, -100, 0);
			} 
		}
	
        x -= xInput;  
        y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
 		       
        rotation = Quaternion.Euler(0, x, 0);
        var position = rotation * Vector3(0.0, 0.0, -distance) + target.position + (Vector3.down * 10);
        
       	transform.rotation = rotation;
		movement = position - transform.position;	
		


		var flags = characterController.Move(movement);


		
		//flags = characterController.Move(movement);
		
		grounded = (flags  CollisionFlags.CollidedBelow) != 0;
    }
}

static function ClampAngle (angle : float, min : float, max : float) {
	if (angle < -360)
		angle += 360;
	if (angle > 360)
		angle -= 360;
	return Mathf.Clamp (angle, min, max);
}