First-person player moves the camera to follow

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.