Unity, how to make the character turn with the mouse sliding?

introduce

Unity, how to make the character turn with the mouse sliding?

insert image description here


method

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;



public class PlayerMovement : MonoBehaviour
{
    
    
	public float speed = 6f;            // 玩家移动速度

	private Vector3 movement;           // 玩家的移动方向
	private Animator playerAC;          // 玩家的动画控制器
	private Rigidbody playerRigidbody; // 玩家的刚体组件

	LayerMask floorMask;


	// 初始化
	void Start()
	{
    
    
		// 获取动画控制器和刚体组件
		playerAC = GetComponent<Animator>();
		playerRigidbody = GetComponent<Rigidbody>();
		
		floorMask = LayerMask.GetMask("floor");
	}

	// 固定时问见新
	void FixedUpdate()
	{
    
    
		float h = Input.GetAxisRaw("Horizontal");
		float v = Input.GetAxisRaw("Vertical");
		// 移动 横向 和纵向
		Move(h, v);
		// 检测是否在移动,播放相应动画
		Animating(h, v);
		turning();
	}

	// 检测是否在移动,播放相应动画
	void Animating(float h, float v)
	{
    
    
		// 只有h不等于0或者v不等于0才应该是移动
		bool walking = h != 0f || v != 0f;
		playerAC.SetBool("iswalking", walking);
	}

	// 移动
	void Move(float h, float v)
	{
    
    
		// 设置移动的方向向量
		movement.Set(h, 0f, v);
		movement = movement.normalized * speed * Time.deltaTime;
		// 使用Rigidbody组件移动玩家
		playerRigidbody.MovePosition(transform.position + movement);
	}
	
	
	
	void turning()
	{
    
    
		Ray cameraRay = Camera.main.ScreenPointToRay(Input.mousePosition);
		RaycastHit cameraHit;
		if (Physics.Raycast(cameraRay, out cameraHit, 100f, floorMask))
		{
    
    
			Vector3 playerToMouse = cameraHit.point - transform.position;
			playerToMouse.y = 0f;
			Quaternion newQuaternion = Quaternion.LookRotation(playerToMouse);
			playerRigidbody.MoveRotation(newQuaternion);
		}
	}

}






This code is a script based on the Unity engine to realize player movement. The main functions are as follows:

  1. Define and initialize variables:
  • speed: player movement speed;
  • movement: the player's movement direction;
  • playerAC: the player's animation controller;
  • playerRigidbody: the rigid body component of the player;
  • floorMask: Floor layer for ray detection.
  1. Implement movement and rotation:
  • Move() function: Receive horizontal and vertical input, set the player's movement direction vector, and then use the Rigidbody component to move the player;
  • The turning() function: Use rays to detect the floor, get the position the player needs to face, and then use the Rigidbody component to rotate the player.
  1. Play animation:
  • Animating() function: Set the iswalking Boolean variable according to whether the player is moving, and then pass it to the animation controller to control the corresponding animation.
  1. Responding to player input:
  • FixedUpdate() function: Detect the player's input within a fixed physical calculation time interval, call the Move() function to move the player, call the Animating() function to play the corresponding animation, and call the turning() function to rotate the player.

This code implements player movement and rotation based on keyboard input, and plays an animation based on whether the player is moving or not.


Guess you like

Origin blog.csdn.net/qq_20179331/article/details/130717135