- Home /
Isometric + Orthographic Player Movement
So I'm currently trying to teach my self unity, as well as a bit more C# by following a guide on youtube, however, this problem has caused me headaches each time I get to a certain problem with character movement.
From what I understand, switching to an orthographic camera at a 45 rotation will keep the "worlds" rotation as far as movement rotation works. So I have my camera setup as mentioned, and pressing W will make my character go up and left.
My goal is to have the player move down when pressing S, up when pressing W, ect.
I've found quite a few ways to do this, but a lot of them come with problems, such as the diagonal being slower, movement being locked because of magnitude.
I'd like to keep my current script from the guide I've been following since I like the results I get when using it; is there a way to fix the player rotation without having to rewrite the whole script? I'm a bit of a beginner so please forgive my ignorance.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
public float walkSpeed = 2;
public float runSpeed = 6;
public float turnSmoothTime = 0.2f;
float turnSmoothVelocity;
public float speedSmoothTime = 0.1f;
float speedSmoothVelocity;
float currentSpeed;
Vector3 forward, right;
Animator animator;
// Use this for initialization
void Start () {
animator = GetComponent<Animator> ();
}
// Update is called once per frame
void Update () {
Vector2 input = new Vector2 (Input.GetAxisRaw ("HorizontalKey"), Input.GetAxisRaw ("VerticalKey"));
Vector2 inputDir = input.normalized;
if (inputDir != Vector2.zero) {
float targetRotation = Mathf.Atan2 (inputDir.x, inputDir.y) * Mathf.Rad2Deg;
transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, turnSmoothTime);
}
bool running = Input.GetKey (KeyCode.LeftShift);
float targetSpeed = ((running) ? runSpeed : walkSpeed) * inputDir.magnitude;
currentSpeed = Mathf.SmoothDamp (currentSpeed, targetSpeed, ref speedSmoothVelocity, speedSmoothTime);
transform.Translate (transform.forward * currentSpeed * Time.deltaTime, Space.World);
float animationSpeedPercent = ((running) ? 1 : .5f) * inputDir.magnitude;
animator.SetFloat ("speedPercent", animationSpeedPercent, speedSmoothTime, Time.deltaTime);
}
}