[Unity C#] Jednoczesne obracanie i poruszanie gracza
Ostatnio zmodyfikowano 2024-09-03 21:08
tBane Temat założony przez niniejszego użytkownika |
[Unity C#] Jednoczesne obracanie i poruszanie gracza » 2024-09-03 20:09:03 Nie mogę jednocześnie obracać postaci i jej przemieszczać do przodu. using System.Collections; using System.Collections.Generic; using Unity.VisualScripting; using UnityEngine;
public class PlayerController : MonoBehaviour { public Rigidbody rb; public Animator animator; private Vector3 movement; private float movementSpeed = 1.0f; private float rotateSpeed = 0.25f; private float animSpeed = 1.0f; void Start() { rb = GetComponent < Rigidbody >(); animator = GetComponent < Animator >(); animator.Play( "idle" ); animator.speed = animSpeed; } void Update() { float rotate = Input.GetAxis( "Horizontal" ); float move = Input.GetAxis( "Vertical" ); transform.Rotate( 0, rotate * rotateSpeed, 0 ); movement = transform.forward * move * movementSpeed; if( movement != Vector3.zero || rotate != 0 ) animator.Play( "walk" ); else animator.Play( "idle" ); } private void FixedUpdate() { rb.MovePosition( rb.position + movement * Time.fixedDeltaTime ); } }
|
|
tBane Temat założony przez niniejszego użytkownika |
» 2024-09-03 20:51:52 Trzeba modyfikować RigidBody w void FixedUpdate();using System.Collections; using System.Collections.Generic; using Unity.VisualScripting; using UnityEngine; using UnityEngine.UIElements;
public class PlayerController : MonoBehaviour { public Rigidbody rigidbody; public Animator animator; private float movementSpeed = 10.0f; private float rotationSpeed = 360.0f; private float animSpeed = 1.0f; private float move; private float rotate; private Vector3 movement; private Quaternion rotation = Quaternion.Euler( 0, 0, 0 ); void Start() { rigidbody = GetComponent < Rigidbody >(); animator = GetComponent < Animator >(); animator.Play( "idle" ); animator.speed = animSpeed; } void Update() { rotate = Input.GetAxis( "Horizontal" ); move = Input.GetAxis( "Vertical" ); movement = transform.forward * move * movementSpeed * Time.deltaTime; rotation = Quaternion.Euler( 0f, rotate * rotationSpeed * Time.deltaTime, 0f ); if( movement != Vector3.zero || rotation != Quaternion.Euler( 0.0f, 0.0f, 0.0f ) ) animator.Play( "walk" ); else animator.Play( "idle" ); } private void FixedUpdate() { rigidbody.MovePosition( rigidbody.position + movement ); rigidbody.MoveRotation( rigidbody.rotation * rotation ); } }
|
|
« 1 » |