so im trying to make the player faces a position where the mouse is at but i got an error
code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharactersMovement : MonoBehaviour
{
public float speed;
public Rigidbody2D rb;
// Update is called once per frame
void Update()
{
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
// Debug.Log($"Horizontal input: {horizontal}, Vertical input: {vertical}");
Vector3 movementDirection = new Vector3(horizontal, 0, vertical);
movementDirection.Normalize();
transform.position += movementDirection * speed * Time.deltaTime;
}
}
It’s important that you learn to interpret error messages if you want to progress. We all see many messages every day and being able to understand them is key to success.
Assets\Scripts\CharactersMovement.cs gives you the file name where the error occurred
(42,27) gives you the line number and position in the line where the error occurred.
error CS0034 is the C# error number. You can nearly always search for just CSxxxx and get an explanation online.
Operator ‘-’ is ambiguous on operands of type ‘Vector3’ and ‘Vector2’ is the error message.
You didn’t give us the right module or perhaps just not enough of the code for us to see line 42. However, I can guess what has happened. If you try and do this:
someVector3 - someVector2
Then C# will say that it’s not sure which of the three values in the V3 that you want to subtract the 2 values in V2. You can simply get around this by subtracting the individual values yourself manually, guaranteeing that you get what you want.
Often, you can convert the V3 to a V2 using what is called casting. You put the desired data type in brackets before a variable and C# will try and convert it for you. So you could try:
(Vector2) someVector3 - someVector2
Since I can’t see the code that’s failing, it may or may not work but doing the elements individually always will.