Hello.
I have this really weird bug. It’s hard to explain so I made a video.
Here is my code:
using UnityEngine;
using System.Collections;
public class character : MonoBehaviour {
public int health = 100;
public bool isDead = false;
public int XP = 0;
public int hot = 0;
public int cold = 0;
public int coins = 0;
public float speed = 4f;
public float jumpHeight = 200f;
public float chopTime = 4f;
public float curChopTime;
public Transform treeSight;
public Transform jumpRay;
public Transform spawnPoint;
public RaycastHit2D nearTree;
public RaycastHit2D onGround;
// Color
public enum curColor {
white,
orange,
blue,
green,
red
};
public curColor color;
void Update () {
// Movement
if (Input.GetKey (KeyCode.D)) {
transform.Translate(Vector2.right * speed * Time.deltaTime);
}
if (Input.GetKey (KeyCode.A)) {
transform.Translate(-Vector2.right * speed * Time.deltaTime);
}
if (Input.GetKey (KeyCode.RightArrow)) {
transform.Translate(Vector2.right * speed * Time.deltaTime);
}
if (Input.GetKey (KeyCode.LeftArrow)) {
transform.Translate(-Vector2.right * speed * Time.deltaTime);
}
// Tree chopping
Debug.DrawLine(transform.position, treeSight.position, Color.black);
nearTree = Physics2D.Linecast(transform.position, treeSight.position, 1 << LayerMask.NameToLayer("Tree"));
if (Input.GetKey (KeyCode.E) && nearTree) {
curChopTime += Time.deltaTime * 4;
nearTree.transform.gameObject.GetComponentInChildren<ParticleSystem>().Play();
if (curChopTime >= chopTime) {
Destroy(nearTree.transform.gameObject);
}
} else {
curChopTime = 0;
nearTree.transform.gameObject.GetComponentInChildren<ParticleSystem>().Stop();
}
// Jumping
Debug.DrawLine(transform.position, jumpRay.position, Color.green);
onGround = Physics2D.Linecast(transform.position, jumpRay.position, 1 << LayerMask.NameToLayer("Ground"));
if (Input.GetKeyDown (KeyCode.Space) && onGround) {
GetComponent<Rigidbody2D>().AddForce (Vector2.up * jumpHeight);
}
}
}
Please, please, please help me. ;(
- _zeph