Unity can't find OnMove function of New Input System - c#

I have been working on unity's new ınput system and I have issue.I want use OnMove function of Player Input component but I'm getting this error:
MissingMethodException: PlayerMovement.OnMove Due to: Attempted to access a missing member.So my character don't move.How can I fix this ?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMovement : MonoBehaviour
{
[SerializeField] float runSpeed = 10f;
Vector2 moveInput;
Rigidbody2D myRigidbody;
void Start()
{
myRigidbody = GetComponent<Rigidbody2D>();
}
void Update()
{
Run();
}
void OnMove(InputValue value)
{
moveInput = value.Get<Vector2>();
}
void Run()
{
Vector2 playerVelocity = new Vector2(moveInput.x * runSpeed,
myRigidbody.velocity.y);
myRigidbody.velocity = playerVelocity;
}
}

What is a TileVania?
I would expect a InputAction.CallbackContext there I guess.
Sounds to me like the PlayerInput is trying to call your method via SendMessage but the signature is not correct and it should rather be
void OnMove(InputAction.CallbackContext context)
{
moveInput = context.Get<Vector2>();
}

You can fix this by adding the
[RequireComponent(typeof(Rigidbody2D))]
attribute to your PlayerMovement script. This will make sure that your script always has a Rigidbody2D component when it's added to a game object.

Related

Unity accessing rigidBody component with input system

I have just started a new Unity tut using its input system to move a ball. However, the script doesn't seem to be working when I try to move the ball
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem; //Namespace for accessing InputSystem to control ball
public class PlayerController : MonoBehaviour
{
private Rigidbody rb;
private float movementX;
private float movementY;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Onmove(InputValue movementValue)
{
Vector2 movementVector = movementValue.Get<Vector2>();
movementX = movementVector.x;
movementY = movementVector.y;
}
void FixedUpdate()
{
Vector3 movement = new Vector3(movementX, 0.0f, movementY);
rb.AddForce(movement);
}
}
If you did everything else right and the problem is only in your script then changing from Onmove to OnMove should fix problem
Create in Unity a new InputControl with the name PlayerInputActions and into
Inspector select Generate C# class, Unity autogenerate for you Action Maps , Actions and Binding Property, double click on it to watch, than in a new c# script named NewInputSystem
using UnityEngine;
using UnityEngine.InputSystem;
public class NewInputSystem : MonoBehaviour
{
public Rigidbody rb;
public float moveSpeed = 7.0f;
Vector2 moveDirections = Vector2.zero;
public PlayerInputActions playerControls;
private InputAction move;
private void Awake()
{
playerControls = new PlayerInputActions();
}
private void OnEnable()
{
move = playerControls.Player.Move;
move.Enable();
}
private void OnDisable()
{
move.Disable();
}
void Start()
{
rb =gameObject.GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
moveDirections = move.ReadValue<Vector2>();
}
private void FixedUpdate()
{
//change how you want
rb.velocity = new Vector3(moveDirections.y * moveSpeed, 0, moveDirections.x * -moveSpeed);
}
}

My code to move in unity 2D game not working correctly

There is my current code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMovementScript : MonoBehaviour
{
[SerializeField] float runSpeed = 10f;
Vector2 moveInput;
Rigidbody2D rigidbody;
void Start()
{
rigidbody = GetComponent<Rigidbody2D>();
}
void Update()
{
Run();
}
void OnMove(InputValue value){
moveInput = value.Get<Vector2>();
Debug.Log(moveInput);
}
void Run()
{
Vector2 playerVelocity = new Vector2(moveInput.x * runSpeed, rigidbody.velocity.y);
rigidbody.velocity = moveInput;
}
}
But my character still mowing with speed of 2f and can fly up and down (i did it in first version of my code).
It looks like game not loaded newest code and don't know how to fix it.
I have selected auto refresh.

Error when I try to assign a variable to Rigidbody2D

When I try adding Rigidbody2D to the script it keeps giving me the error CS0428: "Cannot convert method group 'GetComponent' to non-delegate type 'Rigidbody2D'. Did you intend to invoke the method?". MAybe its something im not seeing but ive went through it several times and didnt see anything wrong.
using UnityEngine;
public class characterController : MonoBehaviour
{
public Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
rb.AddForce(Vector2.up * 500);
}
}
}
I think you need to add brackets after Rigidbody2D like this:
using UnityEngine;
public class characterController : MonoBehaviour {
public Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
rb.AddForce(Vector2.up * 500);
}
}
}
To avoid this problem again, try reading the error message carefully. For example, this problem could have been easily avoided by reading docs on GetComponents.
You missed the parentheses after GetComponent
using UnityEngine;
public class characterController : MonoBehaviour
{
public Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
rb.AddForce(Vector2.up * 500);
}
}
}
Instead of running the GetComponent() method and setting rb as what it returns, the code tries setting it as a delegate.

The name 'rb' does not exist in the current context?

I am pretty new to c# and unity, I decided to make a test game were you can control the player, the thing is I keep getting this error: 'The name 'rb' does not exist in the current context'. I have no clue what this means, but this is what I have done.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class movement_controller : MonoBehaviour
{
public float Speed = 1.0f;
void Awake()
{
rb = gameObject.AddComponent<Rigidbody2D>() as Rigidbody2D;
rb.bodyType = RigidbodyType2D.Kinematic;
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.W))
{
rb.velocity = (Speed, 0.0f);
}
}
}
You have to assign rb as a variable.
public float Speed = 1.0f;
private Rigidbody2D rb;
void Awake()
{
rb = gameObject.AddComponent<Rigidbody2D>() as Rigidbody2D;
rb.bodyType = RigidbodyType2D.Kinematic;
}
you have only called rb within the awake function, but you have not assigned it as a global variable.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class movement_controller : MonoBehaviour
{
public float Speed = 1.0f;
public RigidBody rb; //<====
void Awake()
{
rb = gameObject.AddComponent<Rigidbody2D>() as Rigidbody2D;
rb.bodyType = RigidbodyType2D.Kinematic;
}

Animator does not contain "IsInTransition"

Based on this Unity Animator API, there's a public function named IsInTransition. but why i cannot use it?
When i try to code in MonoDevelop, the Autocompletion wont work and also build was Error :
Assets/Scripts/CatAnimator.cs(32,18): error CS1061:
Type `Animator' does not contain a definition for `isInTransition'
and no extension method `isInTransition'
of type `Animator' could be found.
Are you missing an assembly reference?
any idea ?
The Complete Code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Animator : MonoBehaviour {
Animator myAnimator;
public float speed = 10.0f;
public float jumpSpeed = 8.0f;
public float gravity = 20.0f;
private Vector3 moveDirection = Vector3.zero;
CharacterController controller;
float currSpeed, Param1;
bool Param2, Param3;
// Use this for initialization
void Start () {
controller = GetComponent<CharacterController> ();
myAnimator = GetComponent<Animator> ();
}
// Update is called once per frame
void Update () {
Param1 = 0;
Param2 = false;
Param3 = false;
if (controller.isGrounded) {
//==
}
if (myAnimator.IsInTransition (0)) { //ERROR HERE...
}
}//==update
}//==class
The problem here was the fact that you are making a class named Animator. However there is an animator class already provided by Unity. When you declare an object of type Animator (Animator myAnimator;), the compiler thinks of your class instead of the class provided by Unity. And in your class there is no IsInTransition() method to use. To fix this simply rename your class to MyAnimator for example.

Categories