Hi,
This is my code
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
void Update () {
if (Input.GetKey (KeyCode.W)) {
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.Euler (0, Camera.main.transform.rotation.y, 0), Time.deltaTime * 10.0f);
transform.Translate (Vector3.forward * Time.deltaTime);
} else if (Input.GetKey (KeyCode.S)) {
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.Euler (0, Camera.main.transform.rotation.y + 180, 0), Time.deltaTime * 10.0f);
transform.Translate (Vector3.forward * Time.deltaTime);
} else if (Input.GetKey (KeyCode.A)) {
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.Euler (0, Camera.main.transform.rotation.y -90, 0), Time.deltaTime * 10.0f);
transform.Translate (Vector3.forward * Time.deltaTime);
} else if (Input.GetKey (KeyCode.D)) {
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.Euler (0, Camera.main.transform.rotation.y + 90, 0), Time.deltaTime * 10.0f);
transform.Translate (Vector3.forward * Time.deltaTime);
}
}
}
As you can see, it works like this :
Movement is based on rotation (the way the character is facing).
I have several problems :
Can’t move in diagonals.
I want the character to move away from the camera when I press ‘W’, towards the camera when I press ‘S’, and to the sides when I press ‘A’ and ‘D’ (I want it to turn to the side).
If you don’t understand what I’m talking about, I want to make the same thirdperson system than in Yandere Simulator :
First of all, you should always put your codes into code tags, so its easier to understan. Secondly never use transform. Translate, because it ignores collision detection. You cant go diagonally, because you put all of the code into an else if chain. I don’t really now, what’s your secknd problem is, if you could explain, what got wrong with that idea, it would be easier to help
gorbit99:
First of all, you should always put your codes into code tags, so its easier to understan. Secondly never use transform. Translate, because it ignores collision detection. You cant go diagonally, because you put all of the code into an else if chain. I don’t really now, what’s your secknd problem is, if you could explain, what got wrong with that idea, it would be easier to help
What can I use instead of transform.Translate that would do the same thing ?
And why does the speed double when I go in diagonals now ?
Rigidbody, you should learn to use it trough a tutorial or something. It uses unitys builtin physics system, and collisions.
Thanks a lot, here is the updated code :
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(CharacterController))]
public class PlayerMovement : MonoBehaviour {
void Start () {
}
void Update () {
if (Input.GetKey (KeyCode.W)) {
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.Euler (0, Camera.main.transform.eulerAngles.y, 0), Time.deltaTime * 10.0f);
CharacterController controller = GetComponent<CharacterController>();
Vector3 forward = transform.TransformDirection(Vector3.forward);
controller.SimpleMove(forward);
}
if (Input.GetKey (KeyCode.S)) {
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.Euler (0, Camera.main.transform.eulerAngles.y + 180, 0), Time.deltaTime * 10.0f);
CharacterController controller = GetComponent<CharacterController>();
Vector3 forward = transform.TransformDirection(Vector3.forward);
controller.SimpleMove(forward);
}
if (Input.GetKey (KeyCode.A)) {
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.Euler (0, Camera.main.transform.eulerAngles.y - 90, 0), Time.deltaTime * 10.0f);
CharacterController controller = GetComponent<CharacterController>();
Vector3 forward = transform.TransformDirection(Vector3.forward);
controller.SimpleMove(forward);
}
if (Input.GetKey (KeyCode.D)) {
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.Euler (0, Camera.main.transform.eulerAngles.y + 90, 0), Time.deltaTime * 10.0f);
CharacterController controller = GetComponent<CharacterController>();
Vector3 forward = transform.TransformDirection(Vector3.forward);
controller.SimpleMove(forward);
}
}
}
Do you know how I can avoid having my speed doubled when I go in diagonals ?
You need to normalise the vector & then multiply it by your speed. Normalising it caps the maximum vector at 1.
I looked it up and I can’t find where to Normalize my vector, which is “forward” in my case.
I don’t use character controllers but I believe that it already has the movement stuff in it. If you have the character controller on the object then it should move without your needing to code the keys, you just access horizontal & vertical input. Putting something like if(horizontal !=0 && vertical !=0) normalise it. Sorry, I don’t have access to my PC or anything I’ve already done so I can’t paste it in.
tedthebug:
I don’t use character controllers but I believe that it already has the movement stuff in it. If you have the character controller on the object then it should move without your needing to code the keys, you just access horizontal & vertical input. Putting something like if(horizontal !=0 && vertical !=0) normalise it. Sorry, I don’t have access to my PC or anything I’ve already done so I can’t paste it in.
Still doesn’t work.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(CharacterController))]
public class PlayerMovement : MonoBehaviour {
public float speed = 1.0f;
void Start () {
}
void Update () {
Vector3 forward = transform.TransformDirection(new Vector3(0, 0, speed));
CharacterController controller = GetComponent<CharacterController>();
if (Input.GetAxisRaw("Horizontal") != 0 && Input.GetAxisRaw("Vertical") != 0) {
forward.Normalize();
}
if (Input.GetAxisRaw("Vertical") == 1) {
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.Euler (0, Camera.main.transform.eulerAngles.y, 0), Time.deltaTime * 10.0f);
controller.SimpleMove(forward);
}
if (Input.GetAxisRaw("Vertical") == -1) {
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.Euler (0, Camera.main.transform.eulerAngles.y + 180, 0), Time.deltaTime * 10.0f);
controller.SimpleMove(forward);
}
if (Input.GetAxisRaw("Horizontal") == -1) {
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.Euler (0, Camera.main.transform.eulerAngles.y - 90, 0), Time.deltaTime * 10.0f);
controller.SimpleMove(forward);
}
if (Input.GetAxisRaw("Horizontal") == 1) {
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.Euler (0, Camera.main.transform.eulerAngles.y + 90, 0), Time.deltaTime * 10.0f);
controller.SimpleMove(forward);
}
}
}