I need help whith my code

It says "error CS1003: Syntax error, ‘,’ expected
here’s my code (anybody know how to fix this)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(Rigidbody))]

public class charactercontroller : MonoBehaviour
{

public float speed 100f;
Rigidbody rb;

void Start()
{
rb = GetComponent();
}

void Update()
{
Vector2 xMov = Vector2(Input.GetAxisRaw(“Horizontal”) * transform.right.x, Input.GetAxisRaw(“Horizonatal”) * transform.right.z);
Vector2 zMov = Vector2(Input.GetAxisRaw(“Vertical”) * transform.forward.x, Input.GetAxisRaw(“Vertical”) * transform.forward.z);

vector2 velocity = (xMov + zMov).normalized * speed * Time.deltaTime;

rb.velocity = new Vector3(velocity.x, rb.velocity.y, velocity.y);
}
}

A bit confusing error if that’s the cause, but vector2 in “vector2 velocity” needs to be capitalized.

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

[RequireComponent(typeof(Rigidbody))]

public class NewBehaviourScript : MonoBehaviour
    {

    public float speed = 100f;
    Rigidbody rb;


    void Start()
        {
        rb = GetComponent<Rigidbody>();
        }


    void Update()
        {
        Vector2 xMov = new Vector2(Input.GetAxisRaw("Horizontal") * transform.right.x, Input.GetAxisRaw("Horizonatal") * transform.right.z);
        Vector2 zMov = new Vector2(Input.GetAxisRaw("Vertical") * transform.forward.x, Input.GetAxisRaw("Vertical") * transform.forward.z);

        Vector2 velocity = (xMov + zMov).normalized * speed * Time.deltaTime;

        rb.velocity = new Vector3(velocity.x, rb.velocity.y, velocity.y);
        }
    }

Here. I changed the name of the class, but you can change that back :slight_smile:

Then the improved version for not allocating memory every update. I left the last velocity line as it is but you probably want to use something else other than setting velocity of rigidbody directly. I set the vectors to zero at the beginning in case you want to take away control from the player. For example in menus. Just add a if statement with a return to quit early and still setting the values to zero.

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

[RequireComponent(typeof(Rigidbody))]

public class NewBehaviourScript : MonoBehaviour
    {
    public float speed = 100f;
    Rigidbody rb;

    void Start()
        {
        rb = GetComponent<Rigidbody>();
        }

    Vector2 xMov;
    Vector2 zMov;
    Vector2 velocity;
    void Update()
        {
        xMov = Vector2.zero;
        zMov = Vector2.zero;

        xMov.x = Input.GetAxisRaw("Horizontal") * transform.right.x;
        xMov.y = Input.GetAxisRaw("Horizonatal") * transform.right.z;

        zMov.x = Input.GetAxisRaw("Vertical") * transform.forward.x;
        zMov.y = Input.GetAxisRaw("Vertical") * transform.forward.z;

        velocity = (xMov + zMov).normalized * speed * Time.deltaTime;

        rb.velocity = new Vector3(velocity.x, rb.velocity.y, velocity.y);
        }
    }

Vector2 is a struct, and not a class. So in this scenario “new” is a syntaxic sugar and no allocation is taking place. There’s no reason to move it outside of function.

1 Like