Hello I’m a newbie around, currently trying to understand how Unity and C# works while experimenting on different game prototypes.
I found this piece of code’s behaviour to my liking in a WASD controller in Isometric View, but it’s almost too complicated for me to understand it. There are if statements everywhere and so on. It takes two objects (1 camera, 1 forward object) to move in a isometric view. I was wondering if there is a way doing that without that forward object (which i didn’t understand why it is there) and recapitulate this code with a simple form. Here it is:
public class Controller : MonoBehaviour {
public Transform target;
public float speed;
public float runingSpeed;
float currentSpeed;
public GameObject GameCamera;
public Transform objectforward;
bool isRuning = false;
Animator anim;
void Start () {
currentSpeed = speed;
target.position = transform.position;
anim = GetComponentInChildren<Animator>();
}
void Update() {
Move();
//target.position = new Vector3(transform.position.x + x, 0, transform.position.z + z) ;
}
void FixedUpdate(){
float h = Input.GetAxisRaw("Horizontal");
float v = Input.GetAxisRaw("Vertical");
Animating(h, v);
if (Input.GetKey(KeyCode.LeftShift)){
bool running = h != 0f || v != 0f;
anim.SetBool("IsRunning", running);
anim.SetBool("IsWalking", false);
}else{
anim.SetBool("IsRunning", false);
}
}
void Move(){
float relativePosx = target.position.x - transform.position.x;
float relativePosz = target.position.z - transform.position.z;
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
if (x >= 1 || x <= -1 || z >= 1 || z <= -1){
Quaternion rotation = Quaternion.LookRotation(new Vector3(relativePosx + 0.0001f, 0, relativePosz + 0.00001f));
transform.rotation = rotation;
}
if (Input.GetKeyDown(KeyCode.LeftShift)){
isRuning = true;
}
if (Input.GetKeyUp(KeyCode.LeftShift)){
isRuning = false;
}
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D)){
transform.Translate((Vector3.forward * Time.deltaTime * currentSpeed));
}
if (Input.GetKey(KeyCode.A) && Input.GetKey(KeyCode.D)){
currentSpeed = 0;
}
if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.S)){
currentSpeed = 0;
}
target.transform.rotation = GameCamera.transform.rotation;
target.transform.eulerAngles = new Vector3(0, target.transform.eulerAngles.y, 0);
if (Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.S)){
if (isRuning == false){
currentSpeed = speed;
}else{
currentSpeed = runingSpeed;
}
target.Translate(Vector3.forward * Time.deltaTime * currentSpeed * 4);
}
if (Input.GetKey(KeyCode.D) && !Input.GetKey(KeyCode.A)){
if (isRuning == false){
currentSpeed = speed;
}else{
currentSpeed = runingSpeed;
}
target.Translate(Vector3.right * Time.deltaTime * currentSpeed * 4);
}
if (Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.W)){
if (isRuning == false){
currentSpeed = speed;
}else{
currentSpeed = runingSpeed;
}
target.Translate(Vector3.back * Time.deltaTime * currentSpeed * 4);
}
if (Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.D)){
if (isRuning == false){
currentSpeed = speed;
}else{
currentSpeed = runingSpeed;
}
target.Translate(Vector3.left * Time.deltaTime * currentSpeed * 4);
}
if (Input.GetKeyUp(KeyCode.A) || Input.GetKeyUp(KeyCode.W) || Input.GetKeyUp(KeyCode.S) || Input.GetKeyUp(KeyCode.D)){
target.transform.position = objectforward.transform.position;
if (isRuning == false){
currentSpeed = speed;
}else{
currentSpeed = runingSpeed;
}
}
}
void Animating(float h, float v){
bool walking = h != 0f || v != 0f;
anim.SetBool("IsWalking", walking);
}
}