Mail.ruПочтаМой МирОдноклассникиВКонтактеИгрыЗнакомстваНовостиКалендарьОблакоЗаметкиВсе проекты

Ошибка Unity Я новичок

vvoa21 234f Ученик (75), открыт 5 дней назад
Assets\Scripts\MovePlayer.cs(22,20): error CS0029: Cannot implicitly convert type 'void' to 'object'
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MovePlayer : MonoBehaviour
{
private Rigidbody rb;
public float speed = 0.5f;
private Vector3 moveVector;

public Rigidbody Rb { get => rb; set => rb = value; }

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

void Update()
{
moveVector.x = Input.GetAxis("Horizontal");
moveVector.z = Input.GetAxis("Vertical");
rb.MovePosition(rb.position + moveVector * speed * Time.deltaTime);
}
}
2 ответа
Hardstyle 4 ever! Мудрец (16152) 5 дней назад
Understanding the Error:

The error message "CS0029: Cannot implicitly convert type 'void' to 'object'" indicates that you're trying to assign a value of type void (a function that doesn't return anything) to a variable of type object.

Analyzing the Code:

The problematic line is likely:

C#
rb.MovePosition(rb.position + moveVector * speed * Time.deltaTime);
Use code with caution.

While rb.MovePosition is a valid method to move a Rigidbody, it doesn't return a value. The error arises because you're trying to assign the result of this method call, which is void, to a variable or expression that expects an object.

Resolving the Error:

Since rb.MovePosition directly modifies the Rigidbody's position, you don't need to assign its return value to anything. Simply remove any unnecessary assignments or expressions related to this method call.

Corrected Code:

C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MovePlayer : MonoBehaviour
{
private Rigidbody rb;
public float speed = 0.5f;
private Vector3 moveVector;

public Rigidbody Rb { get => rb; set => rb = value; }

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

void Update()
{
moveVector.x = Input.GetAxis("Horizontal");
moveVector.z = Input.GetAxis("Vertical");
rb.MovePosition(rb.position + moveVector * speed * Time.deltaTime);
}
}
Use code with caution.

Additional Tips:

Rigidbody Physics: Ensure that your Rigidbody component has the correct settings for your desired movement behavior. Experiment with different settings like mass, drag, and angular drag.
Input Handling: Consider using Input.GetAxisRaw for more precise movement control, especially if you want to avoid acceleration and deceleration effects.
Smooth Movement: If you want smoother movement, you might explore techniques like lerping or using Rigidbody physics forces.
By following these suggestions and addressing the error, you should be able to achieve the desired player movement behavior in your Unity project.
vvoa21 234fУченик (75) 5 дней назад
Assets\Scripts\MovePlayer.cs(23,6): error CS1513: } expected


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

public class MovePlayer : MonoBehaviour
{
private Rigidbody rb;
public float speed = 0.5f;
private Vector3 moveVector;

public Rigidbody Rb { get => rb; set => rb = value; }

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

void Update()
{
moveVector.x = Input.GetAxis("Horizontal");
moveVector.z = Input.GetAxis("Vertical");
rb.MovePosition(rb.position + moveVector * speed * Time.deltaTime);
}
Hardstyle 4 ever!Мудрец (16152) 5 дней назад
The error message "CS1513: } expected" indicates that the compiler is expecting a closing curly brace } but hasn't found one. This usually happens due to a missing brace or an extra one in your code.

Looking at the code you provided, there seems to be a missing closing curly brace for the Update function.

Here's the corrected code with the missing closing brace:
Hardstyle 4 ever!Мудрец (16152) 5 дней назад
C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MovePlayer : MonoBehaviour
{
private Rigidbody rb;
public float speed = 0.5f;
private Vector3 moveVector;

public Rigidbody Rb { get => rb; set => rb = value; }

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

void Update()
{
moveVector.x = Input.GetAxis("Horizontal");
moveVector.z = Input.GetAxis("Vertical");
rb.MovePosition(rb.position + moveVector * speed * Time.deltaTime);
}
}
Hardstyle 4 ever!Мудрец (16152) 5 дней назад
а ты, я смотрю, АИ не юзаешь?
Sergio 2.1 Оракул (67348) 5 дней назад
 using System.Collections; 
using System.Collections.Generic;
using UnityEngine;

public class MovePlayer : MonoBehaviour
{
private Rigidbody rb;
public float speed = 0.5f;
private Vector3 moveVector;

public Rigidbody Rb { get => rb; set => rb = value; }

void Awake()
{
rb = GetComponent<Rigidbody>();
if (rb == null)
{
Debug.LogError("Rigidbody component is missing on the player object.");
}
moveVector = Vector3.zero;
}

void Update()
{
moveVector.x = Input.GetAxis("Horizontal");
moveVector.z = Input.GetAxis("Vertical");
rb.MovePosition(rb.position + moveVector * speed * Time.deltaTime);
}
}
Похожие вопросы