About Simple Left and Right movement with this CODE
Hello! I bought an asset and I want just to add simple left and right code to when NPC is facing left sprite flip to Left and when is going right just flip Right. In this asset they are just static elements, so I wanted to modify this. NPC sprite moves in all the screen but i just want a left and right flipping. Where do i have to add a code? Simply I want to know how to add this simple change, without breaking the code. Thanks in advance!
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class NPCController: MonoBehaviour {
private float speed; //NPC horizontal and vertical speed
Rigidbody2D rig; //NPC rigidbody
float vert; //vertical direction of NPC movement
float hori; //horizontal direction of NPC movement
bool canGo = false;
void Update()
{
if(canGo)
{
rig.isKinematic = false;
rig.velocity = new Vector2(hori * speed, vert * speed);
gameObject.transform.rotation = new Quaternion(0, 0, 0, 0);
if (PlayerController.instance.area[(int)(gameObject.transform.localPosition.x * PlayerController.instance.xScale + PlayerController.instance.areaWidth / 2)][(int)(gameObject.transform.localPosition.y * PlayerController.instance.yScale + PlayerController.instance.areaHeight / 2)])
{
Destroy(gameObject);
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613015211im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Input/Animation works fine in Play (Editor) but not in the build 0 Answers
Dialogue script shows previous sentences after a while 0 Answers
MAKE player move in falling position when no button is being pressed. 0 Answers
2D Optimization -> 3D CUBE flattened by ortographic camera or stretched pixel? 0 Answers
How do I make the character rotate with the 2D camera? 0 Answers