hammish
hammish

Reputation: 21

operator '*' cannot be applied for operands of type 'Vector3' and 'Quaternion'

Error: operator '*' cannot be applied for operands of type 'Vector3' and 'Quaternion'

rb.MoveRotation(rb.position *Quaternion.Euler (rotation));

I copied this code out of youtube (it worked properly there ..it was the old version of visual studio I guess)

rotation is Vector3 its unable to convert into Quaternion what should i do?

Here is my code:

using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class PlayerMotor : MonoBehaviour
{
    private Vector3 velocity =Vector3.zero;
    private Vector3 rotation = Vector3.zero;

    private Rigidbody rb;
    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }
    // Get movement from PlayerController Script
    public void Move (Vector3 _velocity)
    {
        velocity = _velocity;
    }
    // Get Rotation from PlayerController Script
    public void Rotate (Vector3 _rotation)
    {
        rotation = _rotation;
    }
    void FixedUpdate()
    {
        PerformMovement();
        PerformRotation();
    }
    // Actually performing the rotation
    void PerformMovement()
    {
        if(velocity!=Vector3.zero)
        {
            rb.MovePosition(rb.position + velocity * Time.fixedDeltaTime);
        }  
    }
    // Actually performing the rotation
    void PerformRotation()
    {
        rb.MoveRotation(rb.position *Quaternion.Euler (rotation));
    }
}

Upvotes: 1

Views: 4734

Answers (1)

derHugo
derHugo

Reputation: 90639

See Quaternion operator *

It can only be used in the order Quaternion * Vector3 which means you rotate the given vector

or on two Quaternion for using the first rotation an on top of it add the second one (order matters!)


In your case the issue is not that it is unable to convert into Quaternion but rather that rb.position is a Vector3.

Most probably you do not want to use the rb.position but rather rb.rotation in

void PerformRotation()
{
    rb.MoveRotation(rb.rotation * Quaternion.Euler (rotation));
}

Upvotes: 3

Related Questions