using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float MoveSpeed = 3f;
public float smoothRotationTime = 0.12f;
public bool enableMobileInputs = false;
float currentVelocity;
float currentSpeed;
float speedVelocity;
Transform cameraTransform;
private Animator ch_animator;
public FixedJoystick joystick;
void Start()
{
cameraTransform = Camera.main.transform;
ch_animator = GetComponent<Animator>();
}
void Update()
{
Vector2 input = Vector2.zero;
if (enableMobileInputs)
{
input = new Vector2(joystick.input.x, joystick.input.y);
ch_animator.SetBool("Walk", true);
}
else
{
input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
ch_animator.SetBool("Walk", false);
}
Vector2 inputDir = input.normalized;
if (inputDir != Vector2.zero)
{
float rotation = Mathf.Atan2(inputDir.x, inputDir.y) * Mathf.Rad2Deg+ cameraTransform.eulerAngles.y;
transform.eulerAngles = Vector3.up *Mathf.SmoothDampAngle(transform.eulerAngles.y,rotation, ref currentVelocity, smoothRotationTime);
}
float targetSpeed = MoveSpeed * inputDir.magnitude;
currentSpeed = Mathf.SmoothDamp(currentSpeed,targetSpeed,ref speedVelocity, 0.1f);
transform.Translate(transform.forward * currentSpeed *Time.deltaTime,Space.World);
}
}
السلام عليكم
اريد اضافة Rigidbodyالى هدا الكود
المشكل الدي لدي هو انه عندما احرك joystick قليلا يتحرك اللاعب بسرعة الى خارج الماب
علما انني ابرمج لعبة على اليونتي للهاتف