using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Playersmovement : MonoBehaviour
{
Rigidbody2D body;
public Rigidbody rb;
float horizontal;
float vertical;
float moveLimiter = 0.7f;
public float runSpeed = 20.0f;
public Camera cam;
Vector2 mousePos;
void Start()
{
body = GetComponent<Rigidbody2D>();
}
void Update()
{
// Gives a value between -1 and 1
horizontal = Input.GetAxisRaw("Horizontal"); // -1 is left
vertical = Input.GetAxisRaw("Vertical"); // -1 is down
mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
}
void FixedUpdate()
{
if (horizontal != 0 && vertical != 0) // Check for diagonal movement
{
// limit movement speed diagonally, so you move at 70% speed
horizontal *= moveLimiter;
vertical *= moveLimiter;
Vector2 lookDir = mousePos - rb.position;
float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg - 90f;
rb.rotation = angle;
}
body.velocity = new Vector2(horizontal * runSpeed, vertical * runSpeed);
}
}
it keeps saying error CS0034: Operator ‘-’ is ambiguous on operands of type ‘Vector2’ and ‘Vector3’
and also it says this error CS0029: Cannot implicitly convert type ‘float’ to ‘UnityEngine.Quaternion’
im quite new to code and new to forum stuff
What line of your code is showing the error? The full error text will tell you which line the error is on.
Looks like rb Rigidbody is not Rigidbody2D ?
The subtraction thing is line 42, but the issue really is that you’re using a Rigidbody there (its .position is a Vector3, for 3 dimensions), rather than a Rigidbody2D (its .position is a Vector2, which you want in this case). Get rid of line 9 altogether and change anything that references “rb” to “body” instead.
The rotation one is line 44, and it’s the same thing: using a Rigidbody when you should be using a Rigidbody2D. Doing the above should resolve it.