WASD and arrow keys are used for the movement. Clicking on the ground also moves the player to a location, which works fine enough for now. The main camera is set up in a default manner and NOT as a child of the player object. The script for the camera rotates the camera around (what must eventually be) a 2D player sprite with the camera at a 2.5D isometric angle, following the player at a fixed distance/offset/angle. After going through some hectic learning about euler angles, the rotation script for the camera works too, which I could post here, but it doesn’t seem relevant. It works, and I don’t need to change that particular method. Pressing ‘Q’ or ‘E’ rotate the camera 45 degrees in their respective directions. If you’ve played “Don’t Starve” basically these are the same controls I’m trying to replicate.
Suffice, my main camera uses “LookAt” in its scripted component. If I use a “LookAt” component again in my player script it seems to screw up all my WASD control scripting even though it’s supposed to be in relation to the main camera.
For example, when I write the following in C# script:
if (Input.GetKey (KeyCode.W) || Input.GetKey (KeyCode.UpArrow)) {
// ...
Vector3 cameraForwardLine = Camera.main.transform.forward;
cameraForwardLine.y = 0f;
transform.Translate(Camera.main.transform.forward * Time.deltaTime * wasdSpeed);
}
This will work fine without my “LookAt” sprite component script enabled on the player sprite. Everything I’ve tried to get the sprite to constantly look at the camera while the camera rotates (at 45 degree increments) around and follows the player has resulted in not backwards WASD and arrow key movement but alterations in movement depending on the camera’s current rotation. What’s going on? “LookAt” on my player sprite isn’t working. My basic “LookAt” component class attached to my prototype player sprite (a flattened capsule in this case) in C# looks like this (which does get the player sprite facing the camera but messes up the WASD control):
public class LookAtCameraOnOneAxis : MonoBehaviour {
void Update () {
Vector3 groundLevelCameraLine = Camera.main.transform.position;
groundLevelCameraLine.y = 0f;
transform.LookAt(groundLevelCameraLine, ...[?]);
}
}
For one thing I don’t really know what my “WorldUp” parameter should be in this case. I’ve tried Vector3.up and -Vector3.up (for some reason -Vector3.up has been recommended when doing billboards apparently). I’ve also tried not adding a WorldUp parameter, which also doesn’t work. All three basically do the same thing. Forward pressing W is at first back, which almost makes sense if it were straight back. But the player sprite sinks halfway into the ground and looks to steady out moving back. And S swings the sprite up gracefully into the air and steadies out once the sprite is laying completely horizontal and traveling upward. Each rotation of the camera affects the movement differently with this LookAt script enabled too. But after rotating the camera 45 degrees, W is diagonal back, then W is left/right… Perhaps unnecessarily I’m just describing the messed up behavior here.
To simplify, if I disable this LookAt script on the player object my WASD control works as expected currently, along with everything else already scripted. With the sprite LookAt component disabled, forward is forward with respect to the camera no matter what the rotation of the camera just that my player sprite looks in a fixed direction and not at the camera.
My question is basically, can I use this LookAt function to do what I want on the player sprite, is there an easy fix for this, or am I going to have to write a new algorithm myself somehow without using LookAt on my player sprite… ? Thanks in advance if anyone helps out.