![](/img/trans.png)
[英]How can I make a Player object rotate using the A and D keys in Unity 3D?
[英]How can I make the player jump in unity 3d
你好,你能幫忙讓玩家跳起來嗎
還有如何讓玩家蹲下?
這是我的移動腳本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
public CharacterController controller;
public Transform cam;
public float speed = 6f;
public float turnsmoothT = 0.1f;
float turnsmoothV;
void Update()
{
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
if (direction.magnitude >= 0.1f)
{
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnsmoothV, turnsmoothT);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
controller.Move(moveDir.normalized * speed * Time.deltaTime);
}
}
}
..................................................... ..................................................... ..................................................... ..................................................... ..................................................... …………
看看這是否有幫助:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
public CharacterController controller;
public Transform cam;
public float speed = 6f;
public float turnsmoothT = 0.1f;
private float turnsmoothV;
private bool jumping = false;
private float moveDirectionY = 0;
private float gravity = 9.81f;
private void Update()
{
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
Vector3 moveDir = Vector3.zero;
if (jumping && controller.isGrounded)
{
jumping = false;
}
if (Input.GetKeyDown(KeyCode.Space) && !jumping && controller.isGrounded)
{
moveDirectionY = 3f;
jumping = true;
}
if (direction.magnitude >= 0.1f)
{
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnsmoothV, turnsmoothT);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
}
moveDirectionY -= gravity * Time.deltaTime;
moveDir.y = moveDirectionY;
controller.Move(moveDir * speed * Time.deltaTime);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.