H, I'm going to develop a drag and drop game with unity but this error appears, how to fix this error, please!
this is the full error
( error CS1061: 'GameObject' does not contain a definition for 'localPosition' and no accessible extension method 'localPosition' accepting a first argument of type 'GameObject' could be found (are you missing a using directive or an assembly reference?))
this is my code in the C# script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveSystem : MonoBehaviour
{
public GameObject correctForm;
private bool moving; // to check if it is moving or not
private float startPosX;
private float startPosY;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(moving){
Vector3 mousePos;
mousePos= Input.mousePosition;
mousePos = Camera.main.ScreenToWorldPoint(mousePos);
this.gameObject.transform.localPosition = new Vector3(mousePos.x - startPosX, mousePos.y - startPosY, this.gameObject.localPosition.z);
}
}
public void OnMouseUp(){
moving = false;
}
public void OnMouseDown(){
if (Input.GetMouseButtonDown(0))
{
Vector3 mousePos;
mousePos = Input.mousePosition;
mousePos = Camera.main.ScreenToWorldPoint(mousePos);
startPosX = mousePos.x - this.transform.localPosition.x;
startPosY = mousePos.y - this.transform.localPosition.y;
moving = true;
}
}
}
In the update function, in last parameter of last line, you typed this.gameObject.localPosition.z. GameObject has no field named localPosition. You should fix it by this.gameObject.transform.localPosition.z. In conclusion, your Update should looks like this:
// Update is called once per frame
void Update()
{
if(moving){
Vector3 mousePos;
mousePos= Input.mousePosition;
mousePos = Camera.main.ScreenToWorldPoint(mousePos);
this.gameObject.transform.localPosition = new Vector3(mousePos.x - startPosX, mousePos.y - startPosY, this.gameObject.transform.localPosition.z);
}
}
Related
I'm trying to make a character walk in unity but the error pops up that says "error CS1061: 'PlayerInput' does not contain a definition for 'CharacterControls' and no accessible extension method 'CharacterControls' accepting a first argument of type 'PlayerInput' could be found (are you missing a using directive or an assembly reference?)"
I can't seem to find anything that can help, plus I'm really new to C#, so please bear with me.
using UnityEngine;
using UnityEngine.InputSystem;
using System.Collections;
using System.Collections.Generic;
using static System.Numerics.Vector3;
class AnimationAndMovementController : MonoBehaviour
{
PlayerInput playerInput;
CharacterController characterController;
Animator animator;
Vector2 currentMovementInput;
Vector3 currentMovement;
bool isMovementPressed;
float rotationFactorPerFrame = 15.0f;
public bool IsMovementPressed { get => isMovementPressed; set => isMovementPressed = value; }
void Awake()
{
playerInput = new PlayerInput();
characterController = GetComponent<CharacterController>();
animator = GetComponent<Animator>();
playerInput.CharacterControls.Move.started += onMovementInput;
playerInput.CharacterControls.Move.canceled += onMovementInput;
playerInput.CharacterControls.Move.performed += onMovementInput;
}
void handleRotation()
{
Vector3 posititonToLookAt;
posititonToLookAt.z = currentMovement.z;
posititonToLookAt.y = 0.0f;
posititonToLookAt.x = currentMovement.x;
Quaternion currentRotation = transform.rotation;
if (IsMovementPressed)
{
Quaternion targetRotation = Quaternion.LookRotation(posititonToLookAt);
transform.rotation = Quaternion.Slerp(currentRotation, targetRotation, rotationFactorPerFrame * Time.deltaTime);
}
}
void onMovementInput (InputAction.CallbackContext context)
{
currentMovementInput = context.ReadValue<Vector2>();
currentMovement.x = currentMovementInput.x;
currentMovement.z = currentMovementInput.y;
IsMovementPressed = currentMovementInput.x != 0 || currentMovementInput.y != 0;
}
void handleAnimation()
{
bool isWalking = animator.GetBool("isWalking");
if (IsMovementPressed &
!isWalking)
{
animator.SetBool("isWalking", true);
}
else if (!IsMovementPressed & isWalking)
{
animator.SetBool("isWalking", false);
}
}
// Update is called once per frame
void Update()
{
handleRotation();
handleAnimation();
characterController.Move(currentMovement * Time.deltaTime);
}
void OnEnable()
{
playerInput.CharacterControls.Enable();
}
void OnDisable()
{
playerInput.CharacterControls.Disable();
}
}
any help is appreciated!
It looks like you were following a tutorial that was using the new "InputSystem". That means that your PlayerInput has been created for you, based on the details you used when creating your Input bindings. You have to go to your Actions window, which will look like ..
And from there, make sure that your action map is called exactly (spelling counts) "CharacterControls", and that it has a "Move" action, with as many move inputs as you want to include.
The fact that the IDE you are using (Microsoft Visual Studio?) didn't highlight this error before you even got back to the Unity editor makes me think your environment might not be set up correctly?
I just start learn some unity tutorial and get stuck by the error " There is no argument given that corresponds to the required formal parameter 'direction' of 'My_Script.movePlayer(Vector3) "
And I dont know why?
public class My_Script : MonoBehaviour
{
public float speed = 10.0f;//movement speed
private Rigidbody rb;
public Vector3 movement;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
// Keyboard Input
float x = Input.GetAxis("Horizontal");
float y = Input.GetAxis("Jump");
float z = Input.GetAxis("Vertical");
movement = new Vector3(x, y, z);
}
private void Fixedupdate()
{
movePlayer();
}
void movePlayer(Vector3 direction)
{
rb.velocity = direction * speed;
}
}
Your function movePlayer(Vector3 direction) takes a parameter of type vector3, but when you call this function in FixedUpdate(), you forgot to give it this parameter. So you should write :
private void Fixedupdate()
{
movePlayer(movement);
}
This should solve your problem.
Every function which is non-parameterless accepts an argument.
So you will have to pass a Vector3 value inside the movePlayer function, like this:
movePlayer(movement) or something else of type Vector3
I'm making a 2D game with Unity using the new Input System. I used spriteRenderer.flipX to flip the player but since it is made of three parts (body and two eyes), the spriteRenderer didn't work. Also, I couldn't convert them to a single sprite because I need the eyes in order to animate them. So, I decided to use transform.localscale for flipping and change movementInput value from a float to a Vector2. The problem is that when I press the arrow or AD keys to move the character, an error pops up saying "Cannot read value of type Vector2 from composite". This error is from InputActionState which is a long and complicated code related to the new Input System. Also, getting input from gamepad causes the same error. I don't know what does this error mean and how can I fix it. Now, the player can jump but it can't move or flip. You can see the code, the error and my action map down below.
This is my Player Controller:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
[SerializeField] private float speed, jumpSpeed;
[SerializeField] private LayerMask ground;
private PlayerActionControls playerActionControls;
private Rigidbody2D rb;
private PolygonCollider2D pol;
private Animator animator;
private bool facingRight = true;
private Vector2 movementInput;
private void Awake() {
playerActionControls = new PlayerActionControls();
rb = GetComponent<Rigidbody2D>();
pol = GetComponent<PolygonCollider2D>();
animator = GetComponent<Animator>();
}
private void OnEnable() {
playerActionControls.Enable();
}
private void OnDisable() {
playerActionControls.Disable();
}
void Start()
{
playerActionControls.Land.Jump.performed += ctx => Jump(ctx.ReadValue<float>());
}
private void Jump(float val) {
if (val == 1 && IsGrounded()) {
rb.AddForce(new Vector2(0, jumpSpeed), ForceMode2D.Impulse);
}
}
private bool IsGrounded() {
Vector2 topLeftPoint = transform.position;
topLeftPoint.x -= pol.bounds.extents.x;
topLeftPoint.y += pol.bounds.extents.y;
Vector2 bottomRightPoint = transform.position;
bottomRightPoint.x += pol.bounds.extents.x;
bottomRightPoint.y -= pol.bounds.extents.y;
return Physics2D.OverlapArea(topLeftPoint, bottomRightPoint, ground);
}
void FixedUpdate()
{
if(facingRight == false && movementInput.x > 0){
Flip();
} else if (facingRight == true && movementInput.x < 0){
Flip();
}
}
void Flip(){
facingRight = !facingRight;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}
void Update()
{
Vector2 movementInput = playerActionControls.Land.Move.ReadValue<Vector2>();
Vector2 currentPosition = transform.position; // This was a Vector3 but since I got the error "Operator '+=' is ambiguous on operands of type 'Vector3' and 'Vector2", I changed it to a Vector2.
currentPosition += movementInput * speed * Time.deltaTime;
transform.position = currentPosition;
}
}
This is the error I get when I want to move the player.
This is my action map.
I changed my action map to this and now the flipping is working and I don't get that error anymore.
I am making a 2D game where you control a shield and you have to block falling obstacles from reaching base. I know that to move something you use rb.addforce. However, when I do the same for 2D it doesn't work. I imagine that instead of using a Vector3 you use a Vector2, but it gives me these 2 errors: Assets\Movement.cs(16,25): error CS1503: Argument 1: cannot convert from 'float' to 'UnityEngine.Vector2' and this one: Assets\Movement.cs(16,25): error CS1503: Argument 1: cannot convert from 'float' to 'UnityEngine.Vector2' for each time I write the line. Here is my full code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
public Rigidbody2D rb;
public Collider2D collider;
public float moveSpeed = 0.1f;
private void Update()
{
if (Input.GetKey("w"))
{
rb.AddForce(0f, moveSpeed);
Debug.Log("w");
}
if (Input.GetKey("s"))
{
rb.AddForce(0f, moveSpeed);
Debug.Log("s");
}
}
}
You can add a new Vector2 in rb.AddForce (new Vector2(0,moveSpeed));
Here is the updated code:
public class Movement : MonoBehaviour
{
public Rigidbody2D rb;
public Collider2D collider;
public float moveSpeed = 0.1f;
private void Update()
{
if (Input.GetKey("w"))
{
rb.AddForce(new Vector2(0f, moveSpeed));
Debug.Log("w");
}
if (Input.GetKey("s"))
{
rb.AddForce(new Vector2 (0f, moveSpeed));
Debug.Log("s");
}
}
}
Rigidbody2D.AddForce doesn't take two float parameters but rather as usual a Vector2 and a ForceMode2D where the latter has a default value of ForceMode2D.Force if you don't pass it in.
So your code should rather be
// = new Vector2 (0,1) * moveSpeed
// = new Vector2 (0, moveSpeed)
rb.AddForce(Vector2.up * moveSpeed);
...
// I assume in the second case you rather want to go down so
// = new Vector2 (0, -1) * moveSpeed
// = new Vector2 (0, -moveSpeed)
rb.AddForce(Vector2.down * moveSpeed);
Btw I would rather not Debug.Log every frame in Update it's pretty expensive.
I've just started learning Unity 2D with the "How to make a 2D game" course from Brackeys on YouTube about 3 hrs ago. I'm using Unity 2018.4.1f1 on Ubuntu 18.04, and because JS is not supported in my version so I have to use C# instead. But I've encountered this error on the third video: The variable mainCam of GameSetup has not been assigned. This is my code in C#:
GameSetup.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameSetup : MonoBehaviour
{
public Camera mainCam;
public BoxCollider2D topWall, bottomWall, leftWall, rightWall;
public Transform Player1, Player2;
// Start is called before the first frame update
void Start()
{
// topWall = GetComponent<BoxCollider2D>();
// mainCam = GetComponent<Camera>();
// If I uncomment this, there would be a new error: There is no 'Camera' attached to '_GM' game object, but a script is trying to access it.
}
// Update is called once per frame
void Update()
{
// Move each wall to its edge location
topWall.size = new Vector2(mainCam.ScreenToWorldPoint(new Vector3(Screen.width * 2.0f, 0f)).x, 1.0f);
topWall.offset = new Vector2(0f, mainCam.ScreenToWorldPoint(new Vector3(0f, Screen.width, 0f)).y + 0.5f);
}
}
With the help of Google, I've add rb2d.GetComponent<Rigibody2D>() in Start() from the script of the second video under and prevent the error (there are no Start() in the video)
PlayerControls.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControls : MonoBehaviour
{
public KeyCode moveUp, moveDown;
public float speed = 10;
private Rigidbody2D rb2d = new Rigidbody2D();
// Start is called before the first frame update
void Start()
{
rb2d = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(moveUp))
{
Vector3 v = rb2d.velocity;
v.y = speed;
rb2d.velocity = v;
// rb2d.velocity.y = speed;
}
else if (Input.GetKey(moveDown))
{
Vector3 v = rb2d.velocity;
v.y = speed * (-1);
rb2d.velocity = v;
}
else
{
Vector3 v = rb2d.velocity;
v.y = 0;
rb2d.velocity = v;
}
}
}
How can I fix the bug in GameSetup.cs ? I've done exactly what said in the video, but only changed the language from JS to C#
Either assign it null:
public Camera mainCam = null;
But you are most likely missing to attach a camera object to it in the Unity3D editor. Check your inspector and make sure that whatever object that is using this script(GameSetup.cs) has a camera object assigned to its public variable.
Should look something like this. You have to drag and drop a camera object to the Gameobject.