Hello, I’ve been at this for a few hours now. And I’ve partially been able to achieve what I wanted, although not quite.
I’ve been searching for answers for a while, and although there are very similar questions going around, no answers that’ve been provided have actually helped me.
So, basically. I’ve been able to make it so the character I have in the scene, is facing the same way the camera is, kind of. When I rotate the camera, the character follows, but it lags behind, and eventually, the controls in terms of directions will be all off.
I’ve been at this for a few hours, with no luck.
Any help would be appreciated. Here are my two scripts prepared for you.
Camera Script:
using UnityEngine;
using System.Collections;
public class cameraScript : MonoBehaviour
{
public float MouseSensitivity = 1.0f;
public float XRotation;
public float YRotation;
float MinYRotation = -90f;
float MaxYRotation = 90f;
// Use this for initialization
void Start ()
{
InitCamera ();
}
// Update is called once per frame
void Update ()
{
CameraController();
}
void InitCamera()
{
//Finding the Player's transform and assigning the camera's position to the Player's.
GameObject myTarget = GameObject.FindGameObjectWithTag("Player");
Vector3 DesiredPos = new Vector3(myTarget.transform.position.x, transform.position.y, myTarget.transform.position.z);
transform.position = DesiredPos;
transform.parent = myTarget.transform;
}
void CameraController()
{
//Assigning rotations to appopriate Axes
XRotation += Input.GetAxisRaw ("Mouse X") * MouseSensitivity;
YRotation -= Input.GetAxisRaw ("Mouse Y") * MouseSensitivity;
//Clamp
YRotation = Mathf.Clamp (YRotation,MinYRotation, MaxYRotation );
//Actual Rotation
transform.localRotation = Quaternion.Euler (YRotation,XRotation,0);
}
}
Controller Script:
```csharp
*using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
public float MovementSpeed = 5.0f;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
float localRotationY = Camera.main.transform.rotation.y;
float MoveHorizontal = Input.GetAxis ("Horizontal") * Time.deltaTime * MovementSpeed;
float MoveVertical = Input.GetAxis("Vertical") * Time.deltaTime * MovementSpeed;
transform.Translate (MoveHorizontal, 0 , MoveVertical);
transform.localRotation = Quaternion.Euler (0, Camera.main.transform.localEulerAngles.y, 0);
}
}
__```*__