vusing System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Chef : MonoBehaviour
{
[SerializeField] float speed = 3;
Rigidbody2D rb;
Animator animator;
float horizontalValue;
float runSpeedModifier = 2f;
bool isRunning;
bool facingRight = true;
public bool inRange = false;
public Sprite Stove2;
public GameObject Stove;
void Awake()
{
rb = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
}
void Update()
{
if (inRange = true)
// Bool
{
(Input.GetKeyDown(KeyCode.Space));
// Key Input
{
Stove.gameObject.GetComponent<SpriteRenderer>().sprite = Stove2;
// change sprite
}
}
//Store the horizontal value
horizontalValue = Input.GetAxisRaw("Horizontal");
//If LShift is clicked enable isRunning
if (Input.GetKeyDown(KeyCode.LeftShift))
isRunning = true;
//If LShift is released disable isRunning
if (Input.GetKeyUp(KeyCode.LeftShift))
isRunning = false;
}
void FixedUpdate()
{
Move(horizontalValue);
}
void Move(float dir) //Set value of x using dir and speed
{ float xVal = dir * speed * 100 * Time.deltaTime;
//If we are running mulitply with the running modifier (higher)
if (isRunning)
xVal *= runSpeedModifier;
//If we are running mulitply with the running modifier (higher)
//Create Vec2 for the velocity
Vector2 targetVelocity = new Vector2(xVal, rb.velocity.y);
//Set the player's velocity
rb.velocity = targetVelocity;
//If looking right and clicked left (flip to the left)
if(facingRight && dir < 0)
{
transform.localScale = new Vector3(-1, 1, 1);
facingRight = false;
}
//If looking left and clicked right (flip to rhe right)
else if(!facingRight && dir > 0)
{
transform.localScale = new Vector3(1, 1, 1);
facingRight = true;
}
//(0 idle , 4 walking , 8 running)
//Set the float xVelocity according to the x value
//of the RigidBody2D velocity
animator.SetFloat("xVelocity", Mathf.Abs(rb.velocity.x));
}
private void OnTriggerEnter2D(Collider2D trigger)
{
if (trigger.gameObject.name == "Stove")
{
inRange = true;
}
}
private void OnTriggerExit2D(Collider2D trigger)
{
if (trigger.gameObject.name == "Stove")
{
inRange = false;
}
}
}
2 Answers
2Hi, you have just posted code with no explanation as to what problem you are having. What is the expected behavior and what behavior are you experiencing?
When you want to do a comparison, you need to use ==. When you use a single equal sign it will set the variable equal to the right-hand-side value. So this line is not correct:
if (inRange = true)
Since inRange is a bool, you can just include the bool itself as the condition
if(inRange)
I’m not sure if this is a problem or not, but I think it should be
void Update()
{
if (inRange = true && Input.GetKeyDown(KeyCode.Space)) //Bool and KeyInput
{
Stove.gameObject.GetComponent<SpriteRenderer>().sprite = Stove2;
// change sprite
}
}
You should put them both in the if statement, because Input.GetKeyDown is a bool statement.
Or you could nest the if statements and do
void Update()
{
if (inRange = true) //Bool
{
if(Input.GetKeyDown(Keycode.Space)) //Key Input
{
Stove.gameObject.GetComponent<SpriteRenderer>().sprite = Stove2;
// change sprite
}
}
}