I trying to make camera locked while following this YT tutorial. https://youtu.be/jiyOZbKRfaY?si=f65BdfniDsn462AQ but on the line movement =.... is error on Vector3. which I dont get it. also I still new at c# coding for unity. thanks for answer!
using System.Runtime.CompilerServices;
using Unity.Mathematics;
using Unity.VisualScripting;
using UnityEngine;
using Vector3 = UnityEngine.Vector3;
public class Player : MonoBehaviour
{
[SerializeField] private float speed = 2f;
[SerializeField] private float turnspeed = 45f;
[SerializeField] private Transform CameraTransform;
private Animator animator;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
animator = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
float HorizontalInput = Input.GetAxis("Horizontal");
float VerticalInput = Input.GetAxis("Vertical");
var velocity = Vector3.forward * Input.GetAxis("Vertical")* speed;
transform.Translate(velocity * Time.deltaTime);
transform.Rotate(Vector3.up, Input.GetAxis("Horizontal")* Time.deltaTime * turnspeed);
animator.SetFloat("speed", velocity.magnitude);
Vector3 movement = new Vector3(HorizontalInput, 0 ,VerticalInput);
movement = quaternion.AxisAngle(CameraTransform.rotation.eulerAngles.y, Vector3.up)* movement;
}
private void OnApplicationFocus(bool focus)
{
if (focus)
{
Cursor.lockState = CursorLockMode.Locked;
}
}
}
For making camera lock zoom. not zooming auto while moving cursor with cinemachine
tl;dr: You want to use Quaternion.AngleAxis
. Casing matters in c#
quaternion.AxisAngle
(or better the entire type quaternion
is from Unity.Mathematics
which is mainly used by the Burst Compiler.
Even though nonsense to do in your use case, in theory it would still work due to implicit conversions Vector3
-> float3
and also back from resulting quaternion
-> Quaternion
.
However, quaternion.AxisAngle
expects parameters in the order of float3, float
(following the naming) but you are passing in the arguments in the order float, Vector3
=> The first float
is implicitly converted to float3
. But then the compiler complains about your second Vector3
parameter not being able to be converted to the expected float
.
Long story short, as mentioned above: You don't want to use Unity.Mathematics
here! This was probably caused by a typo quaternion
instead of Quaternion
in the first place ;)