GetAxisRaw
How do I use Get Axis Raw
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: JumpAndRunMovement.cs
Copy
68 void UpdateMovement()
69 {
70 Vector2 movementVelocity = m_Body.velocity;
71
72 if( Input.GetAxisRaw( "Horizontal" ) > 0.5f )
73 {
74 movementVelocity.x = Speed;
75
76 }
77 else if( Input.GetAxisRaw( "Horizontal" ) < -0.5f )
78 {
79 movementVelocity.x = -Speed;
80 }
81 else
82 {
83 movementVelocity.x = 0;
84 }
85
86 m_Body.velocity = movementVelocity;
87 }
File name: PickupController.cs
Copy
308 void UpdateSmoothedMovementDirection()
309 {
310 Transform cameraTransform = Camera.main.transform;
311 bool grounded = IsGrounded();
312
313 // Forward vector relative to the camera along the x-z plane
314 Vector3 forward = cameraTransform.TransformDirection(Vector3.forward);
315 forward.y = 0;
316 forward = forward.normalized;
317
318 // Right vector relative to the camera
319 // Always orthogonal to the forward vector
320 Vector3 right = new Vector3(forward.z, 0, -forward.x);
321
322 float v = Input.GetAxisRaw("Vertical");
323 float h = Input.GetAxisRaw("Horizontal");
324
325 // Are we moving backwards or looking backwards
326 if (v < -0.2f)
327 movingBack = true;
328 else
329 movingBack = false;
330
331 bool wasMoving = isMoving;
332 isMoving = Mathf.Abs(h) > 0.1f || Mathf.Abs(v) > 0.1f;
333
334 // Target direction relative to the camera
335 Vector3 targetDirection = h * right + v * forward;
336 // Debug.Log("targetDirection " + targetDirection);
337
338 // Grounded controls
339 if (grounded)
340 {
341 // Lock camera for short period when transitioning moving & standing still
342 lockCameraTimer += Time.deltaTime;
343 if (isMoving != wasMoving)
344 lockCameraTimer = 0.0f;
345
346 // We store speed and direction seperately,
347 // so that when the character stands still we still have a valid forward direction
348 // moveDirection is always normalized, and we only update it if there is user input.
349 if (targetDirection != Vector3.zero)
350 {
351 // If we are really slow, just snap to the target direction
352 if (moveSpeed < walkSpeed * 0.9f && grounded)
353 {
354 moveDirection = targetDirection.normalized;
355 }
356 // Otherwise smoothly turn towards it
357 else
358 {
359 moveDirection = Vector3.RotateTowards(moveDirection, targetDirection, rotateSpeed * Mathf.Deg2Rad * Time.deltaTime, 1000);
360
361 moveDirection = moveDirection.normalized;
362 }
363 }
364
365 // Smooth the speed based on the current target direction
366 float curSmooth = speedSmoothing * Time.deltaTime;
367
368 // Choose target speed
369 //* We want to support analog input but make sure you cant walk faster diagonally than just forward or sideways
370 float targetSpeed = Mathf.Min(targetDirection.magnitude, 1.0f);
371
372 _characterState = PickupCharacterState.Idle;
373
374 // Pick speed modifier
375 if ((Input.GetKey(KeyCode.LeftShift) | Input.GetKey(KeyCode.RightShift)) && isMoving)
376 {
377 targetSpeed *= runSpeed;
378 _characterState = PickupCharacterState.Running;
379 }
380 else if (Time.time - trotAfterSeconds > walkTimeStart)
381 {
382 targetSpeed *= trotSpeed;
383 _characterState = PickupCharacterState.Trotting;
384 }
385 else if (isMoving)
386 {
387 targetSpeed *= walkSpeed;
388 _characterState = PickupCharacterState.Walking;
389 }
390
391 moveSpeed = Mathf.Lerp(moveSpeed, targetSpeed, curSmooth);
392
393 // Reset walk time start when we slow down
394 if (moveSpeed < walkSpeed * 0.3f)
395 walkTimeStart = Time.time;
396 }
397 // In air controls
398 else
399 {
400 // Lock camera while in air
401 if (jumping)
402 lockCameraTimer = 0.0f;
403
404 if (isMoving)
405 inAirVelocity += targetDirection.normalized * Time.deltaTime * inAirControlAcceleration;
406 }
407 }
File name: ThirdPersonController.cs
Copy
131 void UpdateSmoothedMovementDirection()
132 {
133 Transform cameraTransform = Camera.main.transform;
134 bool grounded = IsGrounded();
135
136 // Forward vector relative to the camera along the x-z plane
137 Vector3 forward = cameraTransform.TransformDirection(Vector3.forward);
138 forward.y = 0;
139 forward = forward.normalized;
140
141 // Right vector relative to the camera
142 // Always orthogonal to the forward vector
143 Vector3 right = new Vector3(forward.z, 0, -forward.x);
144
145 float v = Input.GetAxisRaw("Vertical");
146 float h = Input.GetAxisRaw("Horizontal");
147
148 // Are we moving backwards or looking backwards
149 if (v < -0.2f)
150 movingBack = true;
151 else
152 movingBack = false;
153
154 bool wasMoving = isMoving;
155 isMoving = Mathf.Abs(h) > 0.1f || Mathf.Abs(v) > 0.1f;
156
157 // Target direction relative to the camera
158 Vector3 targetDirection = h * right + v * forward;
159
160 // Grounded controls
161 if (grounded)
162 {
163 // Lock camera for short period when transitioning moving & standing still
164 lockCameraTimer += Time.deltaTime;
165 if (isMoving != wasMoving)
166 lockCameraTimer = 0.0f;
167
168 // We store speed and direction seperately,
169 // so that when the character stands still we still have a valid forward direction
170 // moveDirection is always normalized, and we only update it if there is user input.
171 if (targetDirection != Vector3.zero)
172 {
173 // If we are really slow, just snap to the target direction
174 if (moveSpeed < walkSpeed * 0.9f && grounded)
175 {
176 moveDirection = targetDirection.normalized;
177 }
178 // Otherwise smoothly turn towards it
179 else
180 {
181 moveDirection = Vector3.RotateTowards(moveDirection, targetDirection, rotateSpeed * Mathf.Deg2Rad * Time.deltaTime, 1000);
182
183 moveDirection = moveDirection.normalized;
184 }
185 }
186
187 // Smooth the speed based on the current target direction
188 float curSmooth = speedSmoothing * Time.deltaTime;
189
190 // Choose target speed
191 //* We want to support analog input but make sure you cant walk faster diagonally than just forward or sideways
192 float targetSpeed = Mathf.Min(targetDirection.magnitude, 1.0f);
193
194 _characterState = CharacterState.Idle;
195
196 // Pick speed modifier
197 if (Input.GetKey(KeyCode.LeftShift) | Input.GetKey(KeyCode.RightShift))
198 {
199 targetSpeed *= runSpeed;
200 _characterState = CharacterState.Running;
201 }
202 else if (Time.time - trotAfterSeconds > walkTimeStart)
203 {
204 targetSpeed *= trotSpeed;
205 _characterState = CharacterState.Trotting;
206 }
207 else
208 {
209 targetSpeed *= walkSpeed;
210 _characterState = CharacterState.Walking;
211 }
212
213 moveSpeed = Mathf.Lerp(moveSpeed, targetSpeed, curSmooth);
214
215 // Reset walk time start when we slow down
216 if (moveSpeed < walkSpeed * 0.3f)
217 walkTimeStart = Time.time;
218 }
219 // In air controls
220 else
221 {
222 // Lock camera while in air
223 if (jumping)
224 lockCameraTimer = 0.0f;
225
226 if (isMoving)
227 inAirVelocity += targetDirection.normalized * Time.deltaTime * inAirControlAcceleration;
228 }
229
230
231
232 }
File name: ThirdPersonController.cs
Copy
456 public bool IsMoving()
457 {
458 return Mathf.Abs(Input.GetAxisRaw("Vertical")) + Mathf.Abs(Input.GetAxisRaw("Horizontal")) > 0.5f;
459 }
File name: PlayerCtrl.cs
Copy
37 void Update () {
38 isGrounded = Physics2D.OverlapBox(new Vector2(feet.position.x, feet.position.y), new Vector2(boxWidth, boxHeight), 360.0f, whatIsGround);
39
40 float playerSpeed = Input.GetAxisRaw("Horizontal"); // value will be 1, -1 or 0
41
42 playerSpeed *= speedBoost;
43
44 if (playerSpeed != 0)
45 MovePlayer(playerSpeed);
46 else
47 StopMoving();
48
49 if (Input.GetButtonDown("Jump"))
50 Jump();
51
52 if (Input.GetButtonDown("Fire1"))
53 {
54 FireBullets();
55 }
56
57 ShowFalling();
58
59 if (leftPressed)
60 MovePlayer(-speedBoost);
61
62 if (rightPressed)
63 MovePlayer(speedBoost);
64 }
File name: Player.cs
Copy
56 private void Update ()
57 {
58 //If it's not the player's turn, exit the function.
59 if(!GameManager.instance.playersTurn) return;
60
61 int horizontal = 0; //Used to store the horizontal move direction.
62 int vertical = 0; //Used to store the vertical move direction.
63
64 //Check if we are running either in the Unity editor or in a standalone build.
65#if UNITY_STANDALONE || UNITY_WEBPLAYER
66
67 //Get input from the input manager, round it to an integer and store in horizontal to set x axis move direction
68 horizontal = (int) (Input.GetAxisRaw ("Horizontal"));
69
70 //Get input from the input manager, round it to an integer and store in vertical to set y axis move direction
71 vertical = (int) (Input.GetAxisRaw ("Vertical"));
72
73 //Check if moving horizontally, if so set vertical to zero.
74 if(horizontal != 0)
75 {
76 vertical = 0;
77 }
78 //Check if we are running on iOS, Android, Windows Phone 8 or Unity iPhone
79#elif UNITY_IOS || UNITY_ANDROID || UNITY_WP8 || UNITY_IPHONE
80
81 //Check if Input has registered more than zero touches
82 if (Input.touchCount > 0)
83 {
84 //Store the first touch detected.
85 Touch myTouch = Input.touches[0];
86
87 //Check if the phase of that touch equals Began
88 if (myTouch.phase == TouchPhase.Began)
89 {
90 //If so, set touchOrigin to the position of that touch
91 touchOrigin = myTouch.position;
92 }
93
94 //If the touch phase is not Began, and instead is equal to Ended and the x of touchOrigin is greater or equal to zero:
95 else if (myTouch.phase == TouchPhase.Ended && touchOrigin.x >= 0)
96 {
97 //Set touchEnd to equal the position of this touch
98 Vector2 touchEnd = myTouch.position;
99
100 //Calculate the difference between the beginning and end of the touch on the x axis.
101 float x = touchEnd.x - touchOrigin.x;
102
103 //Calculate the difference between the beginning and end of the touch on the y axis.
104 float y = touchEnd.y - touchOrigin.y;
105
106 //Set touchOrigin.x to -1 so that our else if statement will evaluate false and not repeat immediately.
107 touchOrigin.x = -1;
108
109 //Check if the difference along the x axis is greater than the difference along the y axis.
110 if (Mathf.Abs(x) > Mathf.Abs(y))
111 //If x is greater than zero, set horizontal to 1, otherwise set it to -1
112 horizontal = x > 0 ? 1 : -1;
113 else
114 //If y is greater than zero, set horizontal to 1, otherwise set it to -1
115 vertical = y > 0 ? 1 : -1;
116 }
117 }
118
119#endif //End of mobile platform dependendent compilation section started above with #elif
120 //Check if we have a non-zero value for horizontal or vertical
121 if(horizontal != 0 || vertical != 0)
122 {
123 //Call AttemptMove passing in the generic parameter Wall, since that is what Player may interact with if they encounter one (by attacking it)
124 //Pass in horizontal and vertical as parameters to specify the direction to move Player in.
125 AttemptMove
126 }
127 }
GetAxisRaw 262 lượt xem
Gõ tìm kiếm nhanh...