Using System.Collections;
Using System.Collections.Generic;
Using Unityengine;
public class Playerplay:monobehaviour
{
public float movespeed;
Private Vector3 dir;
First person View rotation
Private Transform Camtrans;
Private Vector3 Camang;
public float camheight; Camera height can be adjusted in unity test
Use this for initialization
void Start ()
{
First person View rotation
Initialize the camera's position
Camtrans = Camera.main.transform;
Vector3 startpos = transform.position;
Startpos.y + = Camheight;
Startpos.z + = 1.3f;
Camtrans.position = startpos;
Camtrans.rotation = transform.rotation;
Camang = Camtrans.eulerangles;
}
void Update ()
{
Playermove ();
Rotate ();
}
Player's Movement
private void Playermove ()
{
Float h = input.getaxis ("horizontal");
Float v = input.getaxis ("Vertical");
dir = new Vector3 (h, 0, V);
Transform. Translate (dir * time.deltatime * movespeed, space.self);
}
private void Rotate ()
{
Camera rotates with mouse
Float y = Input.getaxis ("Mouse X");
float x = Input.getaxis ("Mouse Y");
camang.x-= x;
Camang.y + = y * 2.5f;
Camtrans.eulerangles = Camang;
Set the object to the same direction as the camera's Y rotation
Camtrans.position = new Vector3 (this.transform.position.x, CAMTRANS.POSITION.Y, this.transform.position.z);
float Camy = camang.y;
This.transform.eulerAngles = new Vector3 (this.transform.eulerangles.x, Camy, this.transform.eulerangles.z);
Update camera Location
Vector3 startpos = transform.position;
Startpos.y + = Camheight;
Camtrans.position = startpos;
}
}
First-person player moves the camera to follow