Unity场景跳转

一、Unity场景跳转
1.在跳转前需要引用命名空间:
using UnityEngine.SceneManagement
2.代码:
public void jumparound()
{
SceneManager.LoadScene(0);
}
public void Jumparound()
{
SceneManager.LoadScene(1);
}
3.创建画布(Canvers)、图片(Image)、按钮(Button),之后创建空物体(GameObject)
点击GameObject把代码挂载上去,然后点击Button,把GameObject拖入On Click

二、相机移动:
public GameObject followTarget; //followTarget是指将要让Camera跟随哪个GameObject;
public float moveSpeed; //moveSpeed是相机的移动速度;
if (followTarget !=null) //有没有给followTarget指定一个目标GameObject,如果没有指定,则什么都不做,相机不会移动。
{
transfrom.position=Vector3.Lerp(transfrom.position,followTarget.transfrom.position,Time.deltaTime*moveSpeed);
}
//Vector3.Lerp()用来计算相机应该移动到什么位置的方法。

三、射线检测方法:
public LayerMask layerMask; //LayerMask可以指定射线值检测碰到哪个层
private Vector3 currentLookTarget = Vector3.zero; //currentLookTarget表示想让SpaceMarine面对的目标,因为暂时不知道要让他面对哪个目标,将其赋值为Vector3.zero,即原点

void FixUpdate()
{
RaycastHit hit; //定义一个空的RaycastHit
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay(ray.origin, ray.direction * 1000, Color.green); //画出一条射线;
if (Physics.Raycast(ray, out hit, 1000, layerMask, QueryTriggerInteraction.Ignore))
{
if (hit.point != currentLookTarget) {
currentLookTarget = hit.point;}

Vector3 targetPosition = new Vector3(hit.point.x, transform.position.y, hit.point.z);
//目标位置,Y轴的值是SpaceMarine自身Y轴的值
Quaternion rotation = Quaternion.LookRotation(targetPosition - transform.position);
//根据目标位置和SpaceMarine自身位置计算出旋转(Rotation)。
transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * 10.0f);
//使用插值算法让SpaceMarine平滑地从当前角度(Rotation)旋转到目标角度(Rotation)。

四、rigidbody.MovePosition()移动代码
float h = Input.GetAxis(“Horizontal”);
float v = Input.GetAxis(“Vertical”);
rigidbody.MovePosition(transform.position + new Vector3(h, 0, v) * speed * Time.deltaTime);

五、键盘WSAD控制物体的上下左右移动
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow)) //上移
{
transform.Translate(Vector3.up * speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow)) //下移
{
transform.Translate(Vector3.down * speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) //左移
{
transform.Translate(Vector3.left * speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) //右移
{
transform.Translate(Vector3.right * speed * Time.deltaTime);
}
六、碰撞体和刚体的优点

  • 8
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值