"cannot convert from 'UnityEngine.Vector3' to 'UnityEngine.Space" trying to rotate camera with joystick

huangapple go评论65阅读模式
英文:

"cannot convert from 'UnityEngine.Vector3' to 'UnityEngine.Space" trying to rotate camera with joystick

问题

以下是翻译好的部分:

"我想使用摇杆在水平方向上旋转相机,但出现了错误。

无法从 'UnityEngine.Vector3' 转换为 'UnityEngine.Space'

transform.Rotate(Vector3.up, direction * rotationSpeed * Time.deltaTime);

如果我从上面的代码中移除Vector3.up,错误就会消失,我可以使用摇杆移动相机,但它会在垂直方向上旋转。有没有办法修复这个问题?

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

public class RotateCamera : MonoBehaviour
{
    public float rotationSpeed = 50.0f;
    public VariableJoystick variableJoystick;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        //float horizontalInput = Input.GetAxis("Horizontal");
        //transform.Rotate(Vector3.up, horizontalInput * rotationSpeed * Time.deltaTime);
        Vector3 direction = Vector3.forward * variableJoystick.Vertical + Vector3.right * variableJoystick.Horizontal;
        transform.Rotate(Vector3.up, direction * rotationSpeed * Time.deltaTime);
    }
}
英文:

so i want to rotate the camera horizontally with joystick but it is giving me an error.

cannot convert from 'UnityEngine.Vector3' to 'UnityEngine.Space'

transform.Rotate(Vector3.up, direction * rotationSpeed * Time.deltaTime);

if i remove Vector3.up from the code above the error goes away and i can move the camera with joystick but its rotating vertically. Any way to fix this?

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

public class RotateCamera : MonoBehaviour
{
    public float rotationSpeed = 50.0f;
    public VariableJoystick variableJoystick;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        //float horizontalInput = Input.GetAxis("Horizontal");
        //transform.Rotate(Vector3.up, horizontalInput * rotationSpeed * Time.deltaTime);
       Vector3 direction = Vector3.forward * variableJoystick.Vertical + Vector3.right * variableJoystick.Horizontal;
        transform.Rotate(Vector3.up, direction * rotationSpeed * Time.deltaTime);
    }

}

答案1

得分: 1

我猜你想要:

transform.Rotate(Vector3.up * variableJoystick.Horizontal * rotationSpeed * Time.deltaTime);

然后,或者也许更容易阅读的是:

transform.Rotate(0, variableJoystick.Horizontal * rotationSpeed * Time.deltaTime, 0);
英文:

I guess you want

transform.Rotate(Vector3.up * variableJoystick.Horizontal * rotationSpeed * Time.deltaTime);

then, or also maybe easier to read

transform.Rotate(0, variableJoystick.Horizontal * rotationSpeed * Time.deltaTime, 0);

huangapple
  • 本文由 发表于 2023年2月10日 16:21:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75408515.html
  • c#
  • camera
  • rotation
  • unity-game-engine
  • vector

为所有缺失的枚举添加到List中。 go 66
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定

  • 开发者交流平台

    本页二维码