- Home /
Moving a character in relation to camera position?
Hi everybody,
I'm following some tutorials on creating a game from scratch. I'll paste the character movement script below. It takes input from a player input script and mob AI script to control your player and mobs, respectively.
I've been trying to modify the movement to move my character forward backward left right always in relation to the camera. Cant quite seem to get it to work. I'm using snippets from Unity's ThirdPersonController to help me but I'm stuck.
EDIT: I keep cutting this down so the script looks less daunting, I think thats why I'm getting no replies. I'm sure the answer is a quick edit of the script I have left below though.
Thanks!
Here's the script I'm attempting to modify:
using UnityEngine;
using System.Collections;
[RequireComponent (typeof(CharacterController))]
public class AdvancedMovement : MonoBehaviour {
public enum State {
Idle,
Init,
Setup,
Run
}
public enum Turn {
left = -1,
none = 0,
right = 1
}
public enum Forward {
back = -1,
none = 0,
forward = 1
}
Init();
}
private void Init(){
if(!GetComponent<CharacterController>())return;
if(!GetComponent<Animation>())return;
_state = AdvancedMovement.State.Setup;
}
private void ActionPicker(){
_myTransform.Rotate(0, (int)_turn * Time.deltaTime * rotateSpeed, 0);
if(_controller.isGrounded || _isSwimming) {
airTime = 0;
_moveDirection = new Vector3(0, 0, (int)_forward);
// _moveDirection = Vector3.forward * Input.GetAxis("Move Forward");
_moveDirection = _myTransform.TransformDirection(_moveDirection).normalized; //returns a vector of length 1 which we are trying to move
_moveDirection *= walkSpeed;
if(_forward != Forward.none){
if(_isSwimming){
Swim ();
}
else{
if(_run){
_moveDirection *= runMultiplier;
Run();
}
else{
Walk ();
}
}
}
else{
if(_isSwimming){
Swim();
}
else
Idle ();
}
public void MoveMeForward(Forward z){
_forward = z;
}
public void RotateMe(Turn y){
_turn = y;
}
}
also, I'm not sure how to give my script box a vertical slider.. sorry about the length
Answer by Montraydavis · Nov 16, 2012 at 04:08 PM
If you mean having the camera follow the player, you can aimply attach the camera to the player, and position the angle to your degree of desire.
Or am I misunderstanding your question.?
Sorry, I guess my question isn't as clear as I thought.
$$anonymous$$y camera orbits around the character and can be rotated using the mouse.
I'm looking to make my W,A,S,D movement use the camera's rotation. i.e., if the camera is facing east, and you press A, the character rotates left and moves north.
W should always be away from the camera, D would be always towards the camera, A and S would be left and right from the camera's view.
It would use this portion of the stock ThirdPersonController script:
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;a
// 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");
Your answer
Follow this Question
Related Questions
Vibrating/Blur Character 1 Answer
How to use Camera motion blur (oculus rift DK2) 0 Answers
A Whole Bunch of Third-Person Controller Issues 0 Answers
how to position the camera? pls help. 0 Answers
Camera animation into a scene –most efficient way? 2 Answers