Top.Mail.Ru
Ответы
Аватар пользователя
1 неделю назад
от

Вопрос про движение в unity engine

игрок липнет к стенам

вот скрипт движения (писал не я, взял из ассетов)

using System.Collections.Generic;
using UnityEngine;

public class FirstPersonMovement : MonoBehaviour
{
public float speed = 5;

[Header("Running")]
public bool canRun = true;
public bool IsRunning { get; private set; }
public float runSpeed = 9;
public KeyCode runningKey = KeyCode.LeftShift;

new Rigidbody rigidbody;
/// <summary> Functions to override movement speed. Will use the last added override. </summary>
public List<System.Func<float>> speedOverrides = new List<System.Func<float>>();



void Awake()
{
// Get the rigidbody on this.
rigidbody = GetComponent<Rigidbody>();
}

void FixedUpdate()
{
// Update IsRunning from input.
IsRunning = canRun && Input.GetKey(runningKey);

// Get targetMovingSpeed.
float targetMovingSpeed = IsRunning ? runSpeed : speed;
if (speedOverrides.Count > 0)
{
targetMovingSpeed = speedOverrides[speedOverrides.Count - 1]();
}

// Get targetVelocity from input.
Vector2 targetVelocity = new Vector2( Input.GetAxis("Horizontal") * targetMovingSpeed, Input.GetAxis("Vertical") * targetMovingSpeed);

// Apply movement.
rigidbody.linearVelocity = transform.rotation * new Vector3(targetVelocity.x, rigidbody.linearVelocity.y, targetVelocity.y);
}
}

Только авторизированные пользователи могут оставлять свои ответы
Дата
Популярность
Аватар пользователя
Просветленный
1нед

// Get targetVelocity from input.
Vector2 targetVelocity = new Vector2( Input.GetAxis("Horizontal") * targetMovingSpeed, Input.GetAxis("Vertical") * targetMovingSpeed);


заменить пробуй на

Vector3 moveDirection = transform.right*Input.GetAxis("Horizontal") + transform.forward *Input.GetAxis("Vertical");
moveDirection.Normalize();
rigidbody.velocity = new Vector3(moveDirection.x * targetMovingSpeed, rigidbody.velocity.y, moveDirection.z * targetMovingSpeed);

не уверен конечно, что сработает, но пробуй