R
R
Roma Kotolup2021-11-13 21:37:34
C++ / C#
Roma Kotolup, 2021-11-13 21:37:34

How to fix a bug in Quaternion code in Unity 2.4.5?

How to fix a bug in Quaternion code in Unity 2.4.5?
I made code to make the character walk.
Wrote the code "Move":

using UnityEngine;

[RequireComponent (typeof(Rigidbody))]
public class go : MonoBehaviour 
{
    private Camera cam;
    private Rigidbody rb;

    private Vector3 velocity = Vector3.zero;
    private Vector3 rotation = Vector3.zero;
    private Vector3 rotationCamera = Vector3.zero;

    void Start() 
    {
        rb = GetComponent <Rigidbody> ();
        cam = Camera.main;
    }

    public void Move (Vector3 _velocity)
    {
        velocity = _velocity;
    }

    public void Rotate(Vector3 _rotation)
    {
        rotation = _rotation;
    }

    public void RotateCam(Vector3 _rotationCam)
    {
        rotationCamera = _rotationCam;
    }

    void FixedUpdate()
    {
        PerformMove();
        PerformRotate();
    }

    void PerformMove()
    {
       if (velocity != Vector3.zero)
       {
            rb.MovePosition(rb.position + velocity * Time.fixedDeltaTime);
       } 
    }

    void PerformRotate() {
        rb.MoveRotation (rb.rotation + Quaternion.Euler (rotation));
        if (cam != null) {
            cam.transform.Rotate(-rotationCamera);
        }
    }
}

And looking for one "Control":
using UnityEngine;

[RequireComponent(typeof(go))]
public class control : MonoBehaviour {

    [SerializeField]
    private float speed = 5f;
    [SerializeField]
    private float lookspeed = 3f;
    private go motor;

    void Start()
    {
        motor = GetComponent<go>();
    } 

    void Update() {
        float xMov = Input.GetAxisRaw("Horizontal");
        float zMov = Input.GetAxisRaw("Vertical");
        Vector3 movHor = transform.right * xMov;
        Vector3 movVer = transform.forward * zMov;

        Vector3 velocity = (movHor * movVer).normalized * speed;

        motor.Move(velocity);
        float yRot = Input.GetAxis ("Mouse X");

        Vector3 rotation = new Vector3 (of, yRot, of) * lookspeed;
        motor.Rotate(rotation);

        float xRot = Input.GetAxis ("Mouse Y");

        Vector3 camrotation = new Vector3 (xRot, of, of) * lookpeed;

        motor.Rotatecam(cam_rotation);
    }
}

Added to the object, launched.
Error: Operator '+' cannot be applied to operands of type 'Quaternion' and 'Quaternion'.
How to fix?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nekit Medvedev, 2021-11-15
@NIKROTOS

For what you need, there must be a method. look at the appropriate ones in
https://poqxert.ru/blog/unity/tutorials/matematika...

Y
Yura Milevsky, 2021-11-16
@Hackerman1

Perhaps in this version of the unit, not everything is so smooth. Install 2019 or 2020 version and try it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question