unity 入门

一.界面

screne 场景编辑视图
game 游戏预览视图
hierarchy 层级视图
project 项目视图,包含,资源和文件
inspectier 属性

二.运行

project -> scene->MainScene

三.project文件夹介绍

Animator 动画片制作者
Prefab 预制建筑/人物/模型

四.拉入场景

双击打开 project/Assets/scene/scenelevel1

project/Assets/prefabs
把environment,player 拉进hierarchy框(相当于拉入scenelevel1里)
在inspector 框 ,把他们的位置(position)设置为0

五.Animator,加入动画

4.1基础用法

进入Animator 右键新建一个Animator control,改名为player
1.双击player,进入Animator界面中
2.进入model/Characters/player
把Idle (站立)
death (死亡)
move (移动)
拉入 Animator界面中
3.右键框框,可以连接不同的框框
在这里插入图片描述

4.2添加控制

1 制作控制器

基本思路:walking 布尔值为1时 从Idle到move,为0时从move 到idle
1.Animator界面左侧parameters(属性),新建一个walking bool值
2.点击Idle 到 move 的线,点击右边的inspectier/condition 的加号,加入walking ,属性调为true
3.点击move到idle的线,调为false
4.去掉线中的“has a exit time”

到从其他状态到其他状态同理,新建变量,设置线的值

2 将控制器加入player的控制中

将projetc/Animator/player.controler
拉到 hierarchy/player 右侧的inspector 中的controller

六.加入移动控制

6.1添加组件

点击player,右侧inspector,“add compemont”
刚体:
rigidboby
胶囊碰撞器:
capsule collider

调节碰撞器的位置,大小,包裹角色

6.2加入脚本

在arrest/scripts/player中创建 c#

1.移动

using UnityEngine;
using System.Collections;

public class PlayerMove : MonoBehaviour {

	public Rigidbody playerRig;
	public Animator playerAni;

	private float moveSpeed = 5f;
	private Vector3 movement;

	// Use this for initialization
	void Start () {
		//
		playerRig = this.transform.GetComponent<Rigidbody> ();
		//get aniamtor
		playerAni = transform.GetComponent<Animator> ();
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	void FixedUpdate(){
	//获取虚拟轴
		float h = Input.GetAxis("Horizontal");
		float v = Input.GetAxis ("Vertical");
		Move (h, v);
		Animator (h, v);
	}

	void Move(float h,float v){
		movement.Set (h, 0, v);
		movement = movement.normalized * moveSpeed*Time.deltaTime;
		playerRig.MovePosition (transform.position + movement);
	}

	void Animator(float h,float v){
		if (h != 0 || v != 0) {
			playerAni.SetBool ("Walking", true);
		}
		else {
			playerAni.SetBool ("WalKing", false);
		}
	
	}

}

拉入6.2的compemont中

2.方向

using UnityEngine;
using System.Collections;

public class PlayerMove : MonoBehaviour {

	public Rigidbody playerRig;
	public Animator playerAni;

	private float moveSpeed = 3f;
	private Vector3 movement;


	int floorMask;
	// Use this for initialization
	void Start () {
		//
		playerRig = this.transform.GetComponent<Rigidbody> ();
		//get aniamtor
		playerAni = transform.GetComponent<Animator> ();
		floorMask = LayerMask.GetMask ("floor");
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	void FixedUpdate(){
	//获取虚拟轴
		float h = Input.GetAxis("Horizontal");
		float v = Input.GetAxis ("Vertical");
		Move (h, v);
		Animator (h, v);
		Turning ();
	}

	void Move(float h,float v){
		movement.Set (h, 0, v);
		movement = movement.normalized * moveSpeed*Time.deltaTime;
		playerRig.MovePosition (transform.position + movement);
	}

	void Animator(float h,float v){
		if (h != 0 || v != 0) {
			playerAni.SetBool ("Walking", true);
		}
		else {
			playerAni.SetBool ("WalKing", false);
		}
	
	}

	void Turning(){
		Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);
		RaycastHit hitInfo;
		if (Physics.Raycast (camRay,out hitInfo,100f,floorMask)) {
			Vector3 playerToMouse = hitInfo.point - transform.position;
			playerToMouse.y = 0;
			Quaternion newRotation = Quaternion.LookRotation (playerToMouse);
			//Vector3 t = newRotation.eulerAngles;
			playerRig.MoveRotation (newRotation);
		}
	}

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值