- Home /
Make enemy object move left or right direction position,How to move the enemies
I'm a beginner programmer so I'm not sure how to approach this line of code. The error says something about"unity local function not available in c#4 please update" I'm using an older version of Unity which probably why it shows this error.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy_move : MonoBehaviour {
public int enemySpeed;
public int XMoveDirection;
Rigidbody2D rigid2D;
void Start()
{
rigid2D = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update () {
RaycastHit2D hit = Physics2D.Raycast(transform.position, new Vector2(XMoveDirection, 0));
rigid2D.velocity = new Vector2 (XMoveDirection, 0 ) * enemySpeed;
// gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(XMoveDirection, 0) *
enemySpeed;
if (hit.distance < 0.7f && hit.collider != null)
{
// Flip();
if (hit.collider.tag == "Player")
{
Destroy(hit.collider.gameObject);
}
}
/*
void Flip()
{
if (XMoveDirection > 0)
{
XMoveDirection = -1;
}
else
{
XMoveDirection = 1;
}
}
*/
}
}
,I'm a beginner programmer so I'm not sure how to approach this line of code. The error says something about"unity local function not available in c#4 please update" I'm using an older version of Unity which probably why it shows this error.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy_move : MonoBehaviour {
public int enemySpeed;
public int XMoveDirection;
Rigidbody2D rigid2D;
void Start()
{
rigid2D = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update () {
RaycastHit2D hit = Physics2D.Raycast(transform.position, new Vector2(XMoveDirection, 0));
rigid2D.velocity = new Vector2 (XMoveDirection, 0 ) * enemySpeed;
// gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(XMoveDirection, 0) *
enemySpeed;
if (hit.distance < 0.7f && hit.collider != null)
{
// Flip();
if (hit.collider.tag == "Player")
{
Destroy(hit.collider.gameObject);
}
}
/*
void Flip()
{
if (XMoveDirection > 0)
{
XMoveDirection = -1;
}
else
{
XMoveDirection = 1;
}
}
*/
}
}
You're declaring Flip()
inside of Update()
. That isn't allowed in this version of .NET / C#.
Your question header isn't related to the actual question, and this forum isn't the place for basic C# questions. :-\
The enemy object moves but like goes to the left side of the wall and stay on there. I try to make the enemy object move to the right and left between the two walls but might have to change something in the update for the enemy object.
Answer by Mack2fire · Apr 17, 2018 at 03:26 PM
Sorry about that. I always post something in the wrong place just like in Game Maker Studio's page.
But the first time I do that since I'm new here. And could you help me find a place to post this question so that I do not post it here?
Questions are always valid, so we're here to help your continued learning! But some places are better than others for certain questions. :-)
Yes, It did stop having that error thank you so much. I'm still trying to figure out how to make the enemy object movement between the two wall object I had put in the scene.