- Home /
The question is answered, right answer was accepted
Character movement relative to the camera
Hi,
I got a Camera with a MouseOrbit-Script attached, and a Character with a absic controll script, i need help with making my character move relative to the camera, so if he moves forward he walks away from the camera and if he walks back he moves towards it. Even if i turn the camera. So that i would walk a full circle around an objct that i am locked on just by going left or right.
In the end it should behave like in Skyrim that i can controll the mouse and the player seperately but in relation to each other.
So i prepared some code to choose which direction he should face determined on which key is pressed, and the unity basic move script:
if(Input.GetKey ("w") || Input.GetKey ("a") || Input.GetKey ("s") || Input.GetKey ("d"))
{
_characterState = CharacterState.Walking;
if (Input.GetKey ("w"))
{
//go forward
} else if (Input.GetKey ("s"))
{
//go backward
} else if (Input.GetKey ("d"))
{
//go right
}else if (Input.GetKey ("a"))
{
//go left
}
}
if (_characterController.isGrounded)
{
moveDirection = new Vector3(-Input.GetAxis("Horizontal"),0,-Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
if(_characterState == CharacterState.Walking)
{
moveDirection *= walkSpeed;
}else if(_characterState == CharacterState.Running)
{
moveDirection *= runSpeed;
}
if(Input.GetButton("Jump"))
{
moveDirection.y = jumpSpeed;
}
}
moveDirection.y -= gravity * Time.deltaTime;
_characterController.Move (moveDirection * Time.deltaTime);
I tried to put the movement in a method so i could acces it if the pressed key was determined and send the information which direction it should go this way, but this somehow interfered with my animations and i don't know why because it just checks which animation is active right now and thats it... or it was messing with my CharacterStates... somehow...
Thanks in advance :3
PS: Sry for my probably bad english ^^"
As requested here are some Screenshots, even if ther isn't much to show:
So basically you are trying to do 3rd person of I get it correctly? Im not really sure what you mean. Could you post some Screenshots maybe?
As far as I understand it you just want to get 3rd person.
So you dont even need a Script you can just make the Camera a child of the Character and that should do it.
Hope i could help :) -Simple_zomb
And that's exactly not what i want... sure it is supposed to be 3rd person, but i want to controll the Camera with the $$anonymous$$ouse (should have mentioned that i guess) and the character with w,a,s,d... so they are controlled seperately but in relation to each other just like in Skyrim.
You want some screenshots? gonna add them to the main post ^^
$$anonymous$$udos on solving your problem. I'm late on this but I'll post it anyway.
I found Camera relative very easy using Turn left/turn right animations and simply calculating the difference between Vector2 position of the joystick and player facing direction x/z in a vector2 with respect to the angle from the camera axis.
I wrote a pigeon-english tips based 'guide'
http://answers.unity3d.com/questions/497353/camera-relative-movement-1.html
Answer by erlo68 · Mar 10, 2015 at 09:48 PM
THIS WORKS FINE WITH A CHARACTER-CONTROLLER AND A CAMERA WITH UNITY'S MOUSE-ORBIT SCRIPT TO MAKE A THIRD PERSON GAME WHERE I CONTROLL THE CAMERA WITH MY MOUSE AND THE CHARACTER SEPERATELY WITH KEYBOARD OR A GAMEPAD!!!! (For Perople who are searching exactly that!)
Obligatory example video ---> Click Me
So, i solved the problem on my own:
I made the character walk in the right direction and relative to the camera using another movement-script i found:
if (_characterController.isGrounded)
{
Vector3 forward = _camera.transform.TransformDirection(Vector3.forward);
forward.y = 0;
forward = forward.normalized;
Vector3 right = new Vector3(forward.z, 0, -forward.x);
float h = Input.GetAxis("Horizontal");
float v =Input.GetAxis("Vertical");
moveDirection = (h * right + v * forward);
if(_characterState == CharacterState.Walking)
{
moveDirection *= walkSpeed;
}else if(_characterState == CharacterState.Running)
{
moveDirection *= runSpeed;
}
if (Input.GetButton ("Jump"))
{
moveDirection.y = jumpSpeed;
}
}
moveDirection.y -= gravity * Time.deltaTime;
_characterController.Move (moveDirection * Time.deltaTime);
But like that he would still always look in the same direction, so i made a new Method that turns him smoothly in the direction i want him to look, by sending a Vector3 containing that data and the method sets this in relation to the camera:
void getRotation (Vector3 toRotation)
{
Vector3 relativePos = _camera.transform.TransformDirection(toRotation);
relativePos.y = 0.0f;
Quaternion rotation = Quaternion.LookRotation(relativePos);
transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * turnSpeed);
}
I determine the pressed key pretty rudimentary but it works like i want it to so it's ok for me, and he can walk in 8 axes:
if (Input.GetKey ("w") && Input.GetKey ("d"))
{
getRotation(new Vector3(-1f, 0f, -1f));
}else if (Input.GetKey ("w") && Input.GetKey ("a"))
{
getRotation(new Vector3(1f, 0f, -1f));
}else if (Input.GetKey ("s") && Input.GetKey ("d"))
{
getRotation(new Vector3(-1f, 0f, 1f));
}else if (Input.GetKey ("s") && Input.GetKey ("a"))
{
getRotation(new Vector3(1f, 0f, 1f));
}else if (Input.GetKey ("w"))
{
getRotation(new Vector3(0f, 0f, -1f));
} else if (Input.GetKey ("s"))
{
getRotation(new Vector3(0f, 0f, 1f));
} else if (Input.GetKey ("d"))
{
getRotation(new Vector3(-1f, 0f, 0f));
}else if (Input.GetKey ("a"))
{
getRotation(new Vector3(1f, 0f, 0f));
}
may i ask if i can see the whole script for this? i am having trouble with my movements with camera
Here you go..
Camera Script:
var target : Transform;
var distance = 10.0;
var xSpeed = 250.0;
var ySpeed = 120.0;
var y$$anonymous$$inLimit = -20;
var y$$anonymous$$axLimit = 80;
private var x = 0.0;
private var y = 0.0;
@script AddComponent$$anonymous$$enu("Camera-Control/$$anonymous$$ouse Orbit")
function Start () {
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
// $$anonymous$$ake the rigid body not change rotation
if (GetComponent.<Rigidbody>())
GetComponent.<Rigidbody>().freezeRotation = true;
}
function LateUpdate () {
if (target) {
x += Input.GetAxis("$$anonymous$$ouse X") * xSpeed * 0.02;
y -= Input.GetAxis("$$anonymous$$ouse Y") * ySpeed * 0.02;
y = ClampAngle(y, y$$anonymous$$inLimit, y$$anonymous$$axLimit);
var rotation = Quaternion.Euler(y, x, 0);
var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
transform.rotation = rotation;
transform.position = position;
}
}
static function ClampAngle (angle : float, $$anonymous$$ : float, max : float) {
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return $$anonymous$$athf.Clamp (angle, $$anonymous$$, max);
}
i meant about the movement, but thanks for camera too.
Ok, sorry had some trouble uplaoding the movement because its too long... so heres a download-link for the .cs
http://www.file-upload.net/download-11551658/Third_Person_Controller.cs.html
PS: Ignore the animation segments they are far from finished and should be deleted if not needed!
thanks for the help, i will try this tomorrow. Godbless.
Great answer, this is exactly what I was looking for. Thank you so, much!