最开始用float进行表示浮点型,上一章写了输入,关于属于放在update里,物理类型的放在Fixedupdate里。
bool布尔类型,布尔值判断是否即对错。
update里先初始化,左键用a,右键用d
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public Rigidbody rb;
public float forwardForce = 2000f;
public float sideWaysForce = 500f;
bool leftPressed;//布尔值判断是否
bool rightPressed;
void Start()
{
}
void Update()//每帧都运行,输入部分放置在这里
{
leftPressed = Input.GetKey("a");
rightPressed= Input.GetKey("d");
}
private void FixedUpdate()//物理类型
{
rb.AddForce(0,0, forwardForce*Time.deltaTime);//均衡不同电脑帧率
if (Input.GetKey("d"))
{
rb.AddForce(sideWaysForce* Time.deltaTime, 0, 0);
}
if (Input.GetKey("a"))
{
rb.AddForce(-sideWaysForce * Time.deltaTime, 0, 0);
}
}
}
操作相机
可以将相机直接拖到player里边,就可以看见相机跟着player一起移动,但是会随着物体旋转而旋转。那么我们使用脚本控制是最好的解决方式。
在scripts里创建一个c#命名为Followplayer,拖到相机的数值界面
每一帧都要跟着player,先公开一个变量
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowPlayer : MonoBehaviour//CLASS类名
{
public Transform player;
void Start()
{
}
void Update()
{
transform.position=player.position;
}
}
用脚本将player引进去。变成第一人称视角。
再引入一个变量添加一个偏移量.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowPlayer : MonoBehaviour//CLASS类名
{
public Transform player;
public Vector3 offset;//偏移
void Start()
{
}
void Update()
{
transform.position=player.position+offset;
}
}
将相机的位置进行调整,能更好的观察到物体移动。
此时调整后就不再是第一视角。可以自己调整合适的位置。将player1也放出来可以做一个参照物。
今天先到这里,明天接着更新......