Unity error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement - c#

I just started programing and I'm following a tutorial on how to make a game in unity
So i did everything just like the video said but I'm still getting this error
Can please somebody explain what i did wrong?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public float playerSpeed = 500;
public float directionalSpeed = 20;
void Start()
{
// Start is called before the first frame update
}
// Update is called once per frame
void Update() {
#if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WEBPLAYER
.transform.position.y, gameObject.transform.position.z), directionalSpeed * Time.deltaTime);
#endif
float moveHorizontal = Input.GetAxis("Horizontal");
transform.position = Vector3.Lerp(gameObject.transform.position, new Vector3(Mathf.Clamp(gameObject.transform.position.x + moveHorizontal, -2.5f, 2.5f), gameObject
GetComponent<Rigidbody>().velocity = Vector3.forward * playerSpeed * Time.deltaTime;
//Mobile Controls
Vector2 touch = Camera.main.ScreenToWorldPoint(Input.mousePosition + new Vector3(0, 0, 10f));
if (Input.touchCount > 0)
{
transform.position + new Vector3(touch.x, transform.position.y, transform.position.z);
}
}
}

In fact the code that you will get is:
void Update() {
.transform.position.y, ...), directionalSpeed * ...);
}
And this is not valid. That's why.

Related

unity c# issue unable to get perfect position of a enemy in unity c# script using transform.position

when I command or set an enemy to go left and then at reaching a particular state or position i say my enemy to go right but it is not going right and not reaching to perfect position here is the code I think there is no issue in the code but it is not working properly so here are some images of the code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class enem : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.position = transform.position + new Vector3(-3f, 0f, 0f)* 1 * Time.deltaTime;
if (transform.position.x > -29)
{
transform.position = transform.position + new Vector3(3f, 0f, 0f) * 1 * Time.deltaTime;
}
}
}
Before adding any bool logic, try first simply moving your element:
using UnityEngine;
public class SimeMoveMove : MonoBehaviour
{
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
transform.position += new Vector3(3f, 0f, 0f);
}
}
}
I smiled a little the moment I understood what you were trying to do.
I wrote the code imagining the result you were trying to make!
Create a cube, attach this code as component, and run the game.
// Gift for you friend
public class MoveTest : MonoBehaviour
{
private int _direction = 1;
void Start()
{
transform.position = Vector3.zero;
}
void Update()
{
Vector3 position = transform.position;
int direction = ShouldWeSwitchDirection(position.x, 0f);
if (direction != 0)
{
_direction = direction;
}
transform.position += _direction * new Vector3(3f, 0f, 0f) * 3f * Time.deltaTime;
}
private int ShouldWeSwitchDirection(float value, float center = 0f)
{
float direction = Mathf.Sign(value - center);
float distance = Mathf.Abs(value - center);
// If X Position is larger than 29
if (Mathf.Abs(distance) > 29)
{
// If X is on right side
if (direction >= 0)
{
return -1;
}
// If X is on left side.
else
{
return 1;
}
}
else
{
return 0;
}
}
}

I get error CS7036 and I am clueless on how to solve it

I am new to coding and using it for a school project. I have seen some code learning sessions, but my coding abilities are limited.
What I basically want, is an enemy moving up, stopping at a collider and then moving down and stopping at another ground collider and repeat.
At first, I found a script that, should it be used, the object will move up/down until it meets a collider. This is that particular code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerTest : MonoBehaviour
{
public float speed = 10.0f;
public 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()
{
if (Input.GetKeyDown("w"))
{
movement = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
}
}
void FixedUpdate()
{
moveCharacter(movement);
}
void moveCharacter(Vector3 direction)
{
rb.MovePosition(transform.position + (transform.forward * speed * Time.deltaTime));
}
}
I decided to modify it, to satisfy the needs that I mentioned, like this (note that when I tested the above code, it worked normally)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy_Movement : MonoBehaviour
{
public float speed = 0.1f;
public Rigidbody2D rb;
public Vector3 movement;
public Collider2D EnemyCollider;
public Collider2D DirtCollider;
public Collider2D CeilingCollider;
// Start is called before the first frame update
void Start()
{
rb.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
movement = new Vector3(Input.GetAxis("Horizontal"), 1f, Input.GetAxis("Vertical"));
if (EnemyCollider.IsTouching(DirtCollider))
{
moveCharacterUp();
}
if (EnemyCollider.IsTouching(CeilingCollider))
{
moveCharacterDown();
}
}
void moveCharacterUp(Vector3 direction)
{
Vector3 offset = new Vector3(movement.x + transform.position.x, movement.y + transform.position.y, movement.z + transform.position.z);
rb.MovePosition(transform.position + (offset * speed * Time.deltaTime));
}
void moveCharacterDown(Vector3 direction)
{
Vector3 offset = new Vector3(movement.x + transform.position.x, movement.y + transform.position.y, movement.z + transform.position.z);
rb.MovePosition(transform.position - (offset * speed * Time.deltaTime));
}
However, when I try to run it, I get the following message
Assets\Enemy_Movement.cs(32,13): error CS7036: There is no argument given that corresponds to the required formal parameter 'direction' of 'Enemy_Movement.moveCharacterDown(Vector3)'
I know that I might have done an idiotic mistake, but unfortunately, I have searched everywhere but I could not find a proper solution. (And yea, it looks hideous, but it is my first project).
Any help would be appreciated!
Thanks!
Your moveCharacterDown method takes a Vector3 argument, but you're trying to call it without arguments, hence the error. Having said that, after reading through the code, this argument is not used, so the easiest solution would be to remove it:
void moveCharacterUp() // Here
{
Vector3 offset = new Vector3(movement.x + transform.position.x, movement.y + transform.position.y, movement.z + transform.position.z);
rb.MovePosition(transform.position + (offset * speed * Time.deltaTime));
}
void moveCharacterDown() // And here
{
Vector3 offset = new Vector3(movement.x + transform.position.x, movement.y + transform.position.y, movement.z + transform.position.z);
rb.MovePosition(transform.position - (offset * speed * Time.deltaTime));
}
Your problem is how you call the moveCharacterUp and moveCharacterDown methods. Both methods expect a Vector3 as a parameter, but you use them without a parameter.
Since you don't seem to use the direction parameter in the methods anyway, remove them and it should fix the problem.

Why won't my rigid body stop moving on collision?

I'm new to Unity.
Below is my simple character controller C# script. I'm using 3d cubes with box colliders and rigid bodies as both my walls and player. Currently when my player comes into contact with a wall, it just keeps going.
Why is my script not working?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Controller : MonoBehaviour {
public float speed = 180;
private Rigidbody rig;
private Vector3 movement;
// Use this for initialization
void Start () {
rig = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate () {
if (rig.velocity.magnitude <= 0)
{
if (Input.GetKeyUp("up"))
rig.velocity = new Vector3(0, 0, rig.position.z * speed );
else if (Input.GetKeyUp("down"))
rig.velocity = new Vector3(0, 0, rig.position.z * speed * -1);
else if (Input.GetKeyUp("right"))
rig.velocity = new Vector3(rig.position.x * speed, 0, 0);
else if (Input.GetKeyUp("left"))
rig.velocity = new Vector3(rig.position.x * speed * -1, 0, 0);
}
}
void OnCollisionEnter(Collision collision)
{
rig.velocity = Vector3.zero;
}
}
The script above works... My y position was higher than the position of my walls so there was never any collision. I feel dumb. Leaving post up as a reminder of my failure.

Unity jumping fails while going against a wall

Okay im trying to make my object (player) to jump everything is okay until i go against a wall and keep going against (still W is down) i cant jump wen im hitting a wall if i stop walking he will be enable to jump i tried making the walls on touch to make the player to have velocity = zero but it does not work,
i tried to add rigid body to the walls and freezing them in place, trying to make them kinematic does not work too .
I wish wen i go against walls and keep walking against them to be enable to jump.
If you know how i can do that please share thanks .
Here is the move script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveScript : MonoBehaviour {
private float speed;
private float jumpHight;
private float straffeSpeed;
private float fallMultiplier;
private Rigidbody rig;
private Collider coll;
// Use this for initialization
private void Awake()
{
rig = GetComponent<Rigidbody>();
coll = GetComponent<Collider>();
straffeSpeed = 1.5f;
fallMultiplier = 2.5f;
speed = 10f;
jumpHight = 4f;
}
void Start () {
GroundCheck();
}
// Update is called once per frame
void Update () {
Move();
GroundCheck();
BetterFall();
}
private void Move()
{
float hAxis = Input.GetAxis("Horizontal") * straffeSpeed;
float vAxis = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(hAxis, 0, vAxis) * speed * Time.deltaTime;
rig.MovePosition(transform.position + movement);
if (Input.GetKey(KeyCode.Space) && GroundCheck())
{
rig.velocity = Vector3.up * jumpHight;
}
}
private bool GroundCheck()
{
return Physics.Raycast(transform.position, -Vector3.up, coll.bounds.extents.y + 0.2f);
}
private void BetterFall()
{
if(rig.velocity.y < 0)
{
rig.velocity += Vector3.up * Physics.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
}
}
if (Input.GetKeyDown(KeyCode.Space) && GroundCheck())
{
rig.velocity = Vector3.up * jumpHight;
}
I don't think you are doing this quite right. Try this:
if (Input.GetKeyDown(KeyCode.Space) && GroundCheck())
{
rig.AddForce(Vector3.up * jumpHight, ForceMode.Impulse);
}
:-)

2D Collision detection not working

I've been trying to get collision to work but so far no good.
Screenshot Unity
This is my moveBall.cs which I put on my Ball Object.
using UnityEngine;
using System.Collections;
public class moveBall : MonoBehaviour {
float balSnelheid = 1;
void Update () {
transform.Translate (0, balSnelheid * Time.deltaTime, 0);
}
void OnCollisionTrigger2D (Collision2D coll) {
if (coll.gameObject.name == "Brick") {
Destroy(coll.gameObject);
}
}
}
And this is my movePlayer.cs which I put on my Player Object.
using UnityEngine;
using System.Collections;
public class movePlayer : MonoBehaviour {
public float snelheid;
// Use this for initialization
void Start () {
Screen.showCursor = false;
}
// Update is called once per frame
void Update () {
if (transform.position.x + Input.GetAxis ("hor") * snelheid * Time.deltaTime < 2.9) {
if (transform.position.x + Input.GetAxis ("hor") * snelheid * Time.deltaTime > -2.9) {
transform.Translate(Input.GetAxis("hor") * snelheid * Time.deltaTime, 0, 0);
}
}
if (transform.position.x + Input.GetAxis ("hor") * snelheid * Time.deltaTime > 3) {
transform.position = new Vector3(2.9f, -4.7f, 0);
} else if (transform.position.x + Input.GetAxis ("hor") * snelheid * Time.deltaTime < -3) {
transform.position = new Vector3(-2.9f, -4.7f, 0);
}
}
}
If anyone could give me a tip/solution it would help me out a lot!
One issue I see right off the bat is that you are using the RigidBody component rather than the RigidBody2D component. You need to be careful which components you use.
Also, OnTriggerEnter2D() or OnCollisionEnter2D() is what you are looking for, not OnCollisionTrigger2D.
If using the the 3d components was by choice, please look at OnCollisionEnter() or OnTriggerEnter()

Categories