@Kiger

Как сделать вращение вокруг игрока через синус и косинус?

Здравствуйте, хочу сделать вращающийся меч для игры. он должен и вращаться, и двигаться за игроком. вращаться он умеет, а вот движение не знаю как сделать

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

public class knightMove : MonoBehaviour
{
public float Radius;
public float speed;
float moveValue;

private void Update()
{
moveValue += speed * Time.deltaTime;
var x = Mathf.Sin(moveValue) * Radius;
var y = Mathf.Cos(moveValue) * Radius;

transform.position = new Vector3(x, y, 0f);
}
}
  • Вопрос задан
  • 185 просмотров
Решения вопроса 1
@Kiger Автор вопроса
я задал неправильный вопрос. мне нужно было чтобы меч вокруг двигающегося персонажа кружился

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

public class SwordMove : MonoBehaviour
{
public Transform Player;
public float Radius;
public float speed;
float moveValue;

private void FixedUpdate()
{
moveValue += speed * Time.deltaTime;
var x = Mathf.Sin(moveValue) * Radius;
var y = Mathf.Cos(moveValue) * Radius;

transform.position = new Vector3(x, y, 0f) + Player.position;

}
}
Ответ написан
Комментировать
Пригласить эксперта
Ответы на вопрос 1
by_kapt0xa
@by_kapt0xa
Учу кресты катаюсь на велике
я этот видос сделал для другого чувака, но думаю тебе тоже пригодится
видос:
https://youtu.be/QavP2SPtXQA
код:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour //дай скрипту норм назвыание а не "нью бехейвор скрипот"
{
    void Start()
    {
        axis.Normalize();
    }

    [SerializeField] Vector3 axis;//ось вращения
    [SerializeField] float rotation_speed;// градусы в секунду
    void Update()//если update или late update, то используйте deltaTime, если fixed update, то используйте fixed delta time. грубо говоря одно fixed синхронизированно с физическими событиями
    {
        transform.Rotate(axis, rotation_speed * Time.deltaTime);
    }
}


дополнение
спешал 4ю еще видос на 18 минут:
https://www.youtube.com/watch?v=s0VcFiL2_RE
код из видоса:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript1 : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        gameObject.SetActive(false);
        rotation_axis.Normalize();
        rotated_sword = null;
    }

    [SerializeField] Transform rot_hand;
    [SerializeField] Transform usual_hand;
    [SerializeField] float rotation_speed;
    [SerializeField] Vector3 rotation_axis;
    Transform rotated_sword;
    [SerializeField] Transform current_sword;

    public void RotateSword()
    {
        RotateSword(current_sword);
    }

    void RotateSword(Transform sword)
    {
        Debug.Assert(rotated_sword == null);
        gameObject.SetActive(true);
        rotated_sword = sword;
        sword.parent = rot_hand;
        sword.localPosition = new Vector3(0, 0, 0);
        sword.localRotation = new Quaternion(0, 0, 0, 1);

    }

    public void StopRotatingSword()
    {
        Debug.Assert(rotated_sword != null);
        rotated_sword.parent = usual_hand;
        rotated_sword.localPosition = new Vector3(0, 0, 0);
        rotated_sword.localRotation = new Quaternion(0, 0, 0, 1);
        rotated_sword = null;
        gameObject.SetActive(false);
    }

    public bool IsRotatingSword()
    {
        return rotated_sword != null;
    }

    void FixedUpdate()
    {
        transform.Rotate(rotation_axis, rotation_speed * Time.fixedDeltaTime);
    }
}
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы