[Unity3d]How to control your player to move and rotate by using CharacterController

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Unity3d]How to control your player to move and rotate by using CharacterController相关的知识,希望对你有一定的参考价值。

This article mainly talk about how to control move and rotate the player and simulate the gravity by using charactercontroller component.

 

 

now we add a component.just seems like this.

技术分享

 

we set the values to fit the scene,and now we gonna do the next.

 

we attach a new c# script named CharacterControl

here is the code.

 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 public class CharacterControlScript : MonoBehaviour
 5 {
 6     //控制速度
 7     public float moveSpeed = 10.0f;
 8     public float rotateSpeed = 1.0f;
 9     public float jumpSpeed = 4.0f;      //the speed of jump
10     public float gravity = 1;           //the gravity
11 
12     private bool isMainPlayer = false;
13     private Animator personAnimator;
14     private CharacterController cc;
15 
16     public bool isJump;
17     private bool isMove;
18 
19     private CollisionFlags flags;
20 
21     private Vector3 moveDirection;
22 
23 
24     // Use this for initialization
25     void Start()
26     {
27         if (this.gameObject.tag == "Player")
28         {
29             isMainPlayer = true;
30             cc = this.GetComponent<CharacterController>();
31         }
32         personAnimator = gameObject.GetComponent<Animator>();
33     }
34 
35     // Update is called once per frame
36     void Update()
37     {
38         //如果当前脚本的对象是游戏者
39         if (isMainPlayer)
40         {
41             //Press Arrow keys tp move or rotate
42             float h = Input.GetAxis("Horizontal");
43             float v = Input.GetAxis("Vertical");
44             h *= Time.deltaTime * moveSpeed;
45             v *= Time.deltaTime * moveSpeed;
46             transform.Translate(h, 0, v);
47             transform.Rotate(0, h * rotateSpeed, 0);
48 
49             //FIX THIS BUG:Can not move backwards!Its animation only have the forward one
50             if (Mathf.Abs(Input.GetAxis("Vertical")) > 0.1f)
51             {
52                 personAnimator.SetFloat("Speed_f", moveSpeed);
53             }
54 
55             else
56             {
57                 personAnimator.SetFloat("Speed_f", 0);
58             }
59 
60 
61 
62 
63             //press space key to jump
64             //here are 2 types of jump ways:standing jumpand running jump
65             if (Input.GetKeyDown(KeyCode.Space) && !isJump)
66             {
67                 personAnimator.SetBool("Jump_b", true);
68 
69                 isJump = true;
70                 moveDirection = transform.TransformDirection(moveDirection);
71                 moveDirection.y = jumpSpeed;
72 
73             }
74             else if (Input.GetKeyUp(KeyCode.Space))
75             {
76                 personAnimator.SetBool("Jump_b", false);
77             }
78 
79             //if (isJump)
80             ///{
81             //simulate the fall physic
82             moveDirection.y -= gravity * Time.deltaTime;
83             flags = cc.Move(moveDirection * Time.deltaTime);
84 
85             //when hit the ground
86             if (flags == CollisionFlags.Below)
87             {
88                 isJump = false;
89             }
90             // }
91         }
92 
93     }
94 }

 

 

now we can control the player by press arrow buttons and space to jump.

以上是关于[Unity3d]How to control your player to move and rotate by using CharacterController的主要内容,如果未能解决你的问题,请参考以下文章

How to use GITHUB to do source control

How to Install The Alpha Control Packages.

Unity3d调用iOS陀螺仪

How to:Customize Action Controls 如何:自定义按钮控件

Role-based Access Control vs Attribute-based Access Control: How to Choose

xbox android 蓝牙,How to Connect the Xbox Series X or S Controller to Android