- Home /
[ C#] Character Movement based on Camera Direction.
I'm new to Unity as of coming from Game Maker to learn something new and try to make a 3D game for the first time. And I wanted to try to make a 3 dimensional platformer that jumps and moves around in 8 directions. At first it was difficult (figuring out damn rotations and so on. But still a noob so forgive me lol). But I cant figure out how to make the players movement based on the cameras direction. I have searched through the forums and have found out most people list the Camera into a variable which is called within the script and they add it into their movement script. I thought it would be easy enough but no matter what I do, Unity just likes to cock block me with a bunch of error messages saying that I cant do what I wrote in the script. The function lists on Unitys website doesnt do a whole to explain what they do in the context of a script and there videos are so few. (Most tutorials online are just people copying and pasting assets when all I want to do is just make codes from scratch and actually learn.) So now I am stuck and resulting to getting help online.
All I want is the movement in all 8 directions like a typical Playstation/Xbox game, nice and simple that works with the Cameras facing direction. Ive heard about how local axis and global axis are a thing but from what I looked up no one has broken it down into pure English on how to do anything with it and just said "just do it".
Anyways, Here is my player script. I will post up the camera script if that is necessary as well. But from what Ive looked up its mainly the movement script that is required to edit. Tell me if I'm wrong because again, I'm new to Unity so any new information would be wonderful.
Player Movement:
[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(Animator))]
public class PlayerMovement : MonoBehaviour {
//Variables.
Animator Anim;
Rigidbody rBody;
Camera cameraobject;
CharacterController controller;
//For Movement.
public int Walkspeed = 0;
public int Runspeed = 10;
public Vector3 Vertical;
public Vector3 Horizontal;
public Vector3 Stop;
//For Jumping
public float Gravity = 50.0f; // Gravity
private float JumpSpd = 2f; // Vertical Velocity
public float JumpForce = 10.0f; // Force
void Awake () {
//Required Shit.
Anim = GetComponent<Animator> ();
rBody = GetComponent<Rigidbody> ();
cameraobject = Camera.main;
controller = GetComponent<CharacterController> ();
}
void Start () {
//Movement Variables
Vertical = cameraobject.transform.InverseTransformDirection (Vector3.forward);
Horizontal = transform.TransformDirection (Vector3.right);
Stop = transform.TransformDirection(Vector3.zero);
}
// Update is called once per frame
void Update () {
Move ();
Jump ();
}
// Updated constantly even if frame rate skips.
void FixedUpdate() {
}
//3D Movement.
void Move () {
//Traditional Up, Down, Left, Right Movement.
//If on the Ground.
// Animation.
Anim.SetFloat ("Vertical_Movement", Input.GetAxis ("Vertical"));
Anim.SetFloat ("Horizontal_Movement", Input.GetAxis ("Horizontal"));
/*
if Stop; {
Anim.SetFloat ("Stop", 1);
}
*/
//Forward
if (Input.GetAxis ("Vertical") > 0) {
controller.Move (Vertical * Time.deltaTime * Walkspeed);
transform.localEulerAngles = new Vector3 (0, 180, 0);
//Backward
} else if (Input.GetAxis ("Vertical") < 0) {
controller.Move (-Vertical * Time.deltaTime * Walkspeed); //Actual Movement
transform.localEulerAngles = new Vector3 (0, 0, 0); // Snap Rotations
}
//Left
if (Input.GetAxis ("Horizontal") > 0) {
controller.Move (Horizontal * Time.deltaTime * Walkspeed);
transform.localEulerAngles = new Vector3 (0, 270, 0);
//Right
} else if (Input.GetAxis ("Horizontal") < 0) {
controller.Move (-Horizontal * Time.deltaTime * Walkspeed);
transform.localEulerAngles = new Vector3 (0, 90, 0);
}
//Angled movement
// Up Right.
if (Input.GetAxis ("Vertical") > 0 && Input.GetAxis ("Horizontal") > 0) {
controller.Move (Horizontal * Time.deltaTime / Walkspeed);
controller.Move (Vertical * Time.deltaTime / Walkspeed);
transform.localEulerAngles = new Vector3 (0, 310, 0);
//Down Left
} else if (Input.GetAxis ("Vertical") < 0 && Input.GetAxis ("Horizontal") < 0) {
controller.Move (-Horizontal * Time.deltaTime / Walkspeed);
controller.Move (-Vertical * Time.deltaTime / Walkspeed);
transform.localEulerAngles = new Vector3 (0, 45, 0);
}
//Up Left
if (Input.GetAxis ("Vertical") > 0 && Input.GetAxis ("Horizontal") < 0) {
controller.Move (-Horizontal * Time.deltaTime / Walkspeed);
controller.Move (Vertical * Time.deltaTime / Walkspeed);
transform.localEulerAngles = new Vector3 (0, 310, 0);
//Down Right.
} else if (Input.GetAxis ("Vertical") < 0 && Input.GetAxis ("Horizontal") > 0) {
controller.Move (Horizontal * Time.deltaTime / Walkspeed);
controller.Move (-Vertical * Time.deltaTime / Walkspeed);
transform.localEulerAngles = new Vector3 (0, 45, 0);
}
}
//To Jump
void Jump() {
//Check if Player is on the Ground.
if (controller.isGrounded) {
JumpSpd = -Gravity * Time.deltaTime;
//Then Jump if he is on the Ground.
if (Input.GetButtonDown ("Jump")) {
JumpSpd = JumpForce;
}
} //Else, Fall.
else { JumpSpd -= Gravity * Time.deltaTime; }
//Jump.
Vector3 moveVector = new Vector3 (0, JumpSpd, 0);
controller.Move (moveVector * Time.deltaTime);
}
}
Answer by IronKnigh · Aug 20, 2018 at 01:34 AM
Although this question was asked approximately 2-3 years ago, I recommend you check out my post where the first part of my solution applies to your question:
If the first solution above doesn't apply to you, I recommend you watch this YouTube video.
Ok, so another solution if the one above doesn't apply to you:
https://www.youtube.com/watch?v=_DmUL9$$anonymous$$$$anonymous$$9Yk
Hope this helps!