Hello everyone,
I’m pretty new to Unity and have looked through several resources, but haven’t been able to find an answer to this question so any help would be appreciated.
I’m looking for a way to have a fixed third person camera and move the character relative to the angle of the camera, think early Resident Evil games. So far I’ve imported the default character and attached a third person controller to him. I’ve disabled the third person camera on the controller and set up another fixed camera that I want to use. From this point I’m a little lost at how I should proceed. Can anyone point me in the right direction?
Thanks,
Jason
In early Resident Evil games, up on the d-pad moved the character forward regardless of the camera position, IIRC.
I would just make a modified FPSInputController but make the horizontal axis (a and d) rotate the charactercontroller instead of strafe.
To start, welcome to the forum.
I would back up a little bit. Make a box, and a fixed position camera (Main Camera). Then do some simple scripts to control the box.
var speed=10.0;
function Update(){
var move=Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"),0);
// dont worry about a jump right now.
transform.Translate(move * speed * Time.deltaTime);
}
This is an incredibly basic move script. But it isnt what you really want… You want to control the rotation and position by a 3rd perspective. So you want to transform the move direction into a look direction based off of the camera.
In the second code, we do this translation, then rotate the player towards it and then do the translation of the character towards that direction.
var speed=10.0;
function Update(){
var move=Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"),0);
// dont worry about a jump right now.
var look=Camera.main.transform.TransformDirection(move);
transform.LookAt(transform.position + look);
transform.Translate(move * speed * Time.deltaTime);
}
The code is basic, but if you look at it closely, you can see that the “look” variable contains the direction we are going to move. You apply that direction to a Character controller, and you get the same principle.
This is ripped and edited from my current script in a bit of a hurry, should work, though.
This script should go on your character controller.
var moveDirection = Vector3.zero; // Direction we're moving in.
var targetDirection = Vector3.zero; // Direction we want to move in.
var rotateSpeed = 5; // Speed to rotate at.
var moveSpeed = 5; // Speed to move at.
var controller : CharacterController;
function Awake()
{
controller = GetComponent(CharacterController);
}
function Update()
{
// Get forward direction of the player object.
moveDirection = transform.TransformDirection(Vector3.forward);
// Get forward direction of the camera.
var forward = Vector3.Normalize(Vector3.Scale(camera.main.transform.TransformDirection(Vector3.forward), Vector3(1, 0, 1)));
var right = Vector3(forward.z, 0, -forward.x);
// Blend the two so movement tries to be relative to forwards of the camera.
targetDirection = Input.GetAxis("Horizontal") * right + Input.GetAxis("Vertical") * forward;
if (targetDirection != Vector3.zero)
{
moveDirection = Vector3.Lerp(moveDirection, targetDirection, rotateSpeed * Time.deltaTime);
moveDirection = movement.moveDirection.normalized;
}
controller.Move(moveDirection * moveSpeed * Time.deltaTime);
}
Thanks for the quick response guys! I’ll try this out when I get home.
Farfarer, I tried using your script and I get the error: Assets/Movement.js(29,33): BCE0005: Unknown identifier: ‘movement’. I tried playing around with it, but I can’t figure it out. My apologies if this is stupidly simple, I’m a graphic artist by day and only had a couple of classes in programming. I’m trying to get my head around scripting so I can take a more active role in game dev instead of just doing the art.
1 Like
I agree. I’m trying to use this script, but what is ‘movement’?