RPG Movement script

Hello, I am making an RPG game, and am trying to come up with a script for movement. what I want is a script that moves the player in the direction that the player is pressing on the arrow keys. Any advice? Is there a better way to do this?

The default character controller handles that, doesn’t it? What exactly is the problem?

I want the character to rotate in the direction that It is moving in, and I have very little experience with character movement, because I generally use the prefab scripts

I want to learn how to do this on my own, is there a way for me to teach myself?

Do you mean you want the horizontal axis (a and d keys) to rotate the character instead of strafe? Or you want him to rotate toward the mouse position? Name a game you want to emulate the control style of

no, if i press D, I want the player to go right, and rotate the character to face the right, left, moves to the left, rotates the character to cace the left, ect.

Doesn’t the 3rd person controller prefab in the standard assets do exactly that?

i dont know? maybe, ill check it out

that requires animations, and id rather not screw around with it right now, id just like to start from scratch, for experience

It doesn’t require animations, it just uses some; but the animations have nothing to do with the actual movement/rotation going on in the script. Here’s a similar script from one of the example projects in the resources section; it is basically a stripped down version of that one, with no references to animation (be sure to have the main camera in the scene with some sort of camera script [springfollow would work for testing purposes]):

var speed = 3.0;
var jumpSpeed = 8.0;
var gravity = 20.0;

var smoothSpeed = 10.0;
var smoothDirection = 10.0;

var canJump = true;

private var moveDirection = Vector3.zero;
private var verticalSpeed = 0.0;
private var moveSpeed = 0.0;

private var grounded : boolean = false;
private var jumping : boolean = false;

private var targetAngle = 0.0;

// Require a character controller to be attached to the same game object
@script RequireComponent(CharacterController)

function Awake ()
{
	moveDirection = transform.TransformDirection(Vector3.forward);
}

function UpdateSmoothedMovementDirection ()
{
	var cameraTransform = Camera.main.transform;
	
	// 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);

	// Target direction relative to the camera
	var targetDirection = Input.GetAxis("Horizontal") * right + Input.GetAxis("Vertical") * forward;
	
	// 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)
	{
		moveDirection = Vector3.Lerp(moveDirection, targetDirection, smoothDirection * Time.deltaTime);
		moveDirection = moveDirection.normalized;
	}

	// Smooth the speed based on the current target direction
	var curSmooth = smoothSpeed * Time.deltaTime;
	// When in air we accelerate slower
	if (!grounded)
	{
		curSmooth *= 0.5;
	}

	moveSpeed = Mathf.Lerp(moveSpeed, targetDirection.magnitude * speed, curSmooth);
}

function Update() {

	UpdateSmoothedMovementDirection();

	if (grounded) {
		verticalSpeed = 0.0;
		
		// Jump		
		if (canJump  Input.GetButton ("Jump")) {
			verticalSpeed = jumpSpeed;
			jumping = true;
			SendMessage("DidJump", SendMessageOptions.DontRequireReceiver);
		}
	}
	// Apply gravity
	verticalSpeed -= gravity * Time.deltaTime;
	
	var movement = moveDirection * moveSpeed + Vector3 (0, verticalSpeed, 0);
	movement *= Time.deltaTime;
	
	// Move the controller
	var controller : CharacterController = GetComponent(CharacterController);
	var flags = controller.Move(movement);
	grounded = (flags  CollisionFlags.CollidedBelow) != 0;

	// Set rotation to the move direction	
	transform.rotation = Quaternion.LookRotation(moveDirection);
	
	// We are in jump mode but just became grounded
	if (grounded  jumping)
	{
		jumping = false;
		SendMessage("DidLand", SendMessageOptions.DontRequireReceiver);
	}
}

function GetSpeed () {
	return moveSpeed;
}

function IsJumping () {
	return jumping;
}

function GetDirection () {
	return moveDirection;
}

You can dissect it to figure out how it works.

It works!! thanks, but i still would have liked to do it myself :(. I’m not so good at dissecting, thats not how I learn

Hmm. If you want a simpler way to cahnge change his direction, you could do this

(In C#)

float turnspeed;

Vector3 angles = transform.angles;

angles.y = Input.GetAxis("Horizontal") * turnspeed;

transform.angles = angles;

In Java, MIGHT be this (i may be wrong, i dont use java)

var turnspeed : float;

transform.angles.y = Input.GetAxis("Horizontal") * turnspeed;

You would put all these funtctions in the update function, if that is a mroe simplistic answer you are looking for.

Check the topic WOW Camera for some real good script examples on RPG character movement.

I believe from the way he is talking he wants a static view camera, not 3d dynamic. When he says, D points left, and not turns left… :wink:

I’m still having problems. the idea you guys had isnt working for my game, its interfering with my other scripts, specifically the camera script is the one interfering, and the movement one isnt like what I want. I want something more like this:

were the camera stays in the same place

You mean you just want it to be above, looking down? How do you want the controls to work?

I want the controls to work like I sated earlier, if I press A, I and it to rotate so it faces the left, and move to the left, and vise versa, for W,S, and D. and yes, I want the camera to do so. I want it to say in the same position at the same angle at all times, I dont want it to rotate with the player.

This is basically the smoothfollow script, but with all the rotation crap removed, pretty easy to understand. Just tweak the distance and height vars to taste, but I use it to achieve a similar camera to what you’re asking for:

// The target we are following
var target : Transform;
// The distance in the x-z plane to the target
var distance = 10.0;
// the height we want the camera to be above the target
var height = 5.0;
// How much we want to dampen the height
var heightDamping = 2.0;

function LateUpdate () {
	// Early out if we don't have a target
	if (!target)
		return;
	
	wantedHeight = target.position.y + height;
	currentHeight = transform.position.y;

	// Damp the height
	currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
	
	// Set the position of the camera on the x-z plane to:
	// distance meters behind the target
	transform.position = target.position;
	transform.position -= Vector3.forward*distance;

	// Set the height of the camera
	transform.position.y = currentHeight;
	
	// Always look at the target
	transform.LookAt (target);
	
}

never mind about movement, I got it