mr.killmeplease
mr.killmeplease
CC#
Created by mr.killmeplease on 4/10/2024 in #help
Why isnt my player being deleted and the scene changed?
I have these 2 scripts here that together should control the enemies ability to kill the player and also send them back to the main menu scene i have set up. But its not working and even chatgpt cant tell me why. using UnityEngine; using UnityEngine.AI; using UnityEngine.SceneManagement; public class RedEnemyAi : MonoBehaviour { public int health = 30; private int damage = 10; // Assuming a constant damage value for simplicity float attackDistance = 2; public Transform player; // Reference to the player NavMeshAgent agent; bool isAttacking = false; void Start() { agent = GetComponent<NavMeshAgent>(); player = GameObject.FindGameObjectWithTag("Player").transform; // Find the player object and set it as the target } void Update() { if (!isAttacking) { if (player == null) { // Find the player object if it's null player = GameObject.FindGameObjectWithTag("Player").transform; } else { float distanceToPlayer = Vector3.Distance(transform.position, player.position); if (distanceToPlayer <= attackDistance) { agent.isStopped = true; // Stop moving isAttacking = true; InvokeRepeating("Attack", 0f, 1f); // Call Attack method repeatedly //Debug.Log("Attack!"); } else { agent.isStopped = false; // Resume moving agent.SetDestination(player.position); // Set player's position as the destination } } } } void Attack() { if (player != null && Vector3.Distance(transform.position, player.position) <= attackDistance)
14 replies
CC#
Created by mr.killmeplease on 11/29/2023 in #help
My player model is invisible because of a line of code that wasnt turning it invisible before
So i am making a 2d platformer, and everything was working. I had the first level done and went to move on to the second level and all of a sudden when i go to play it, i cant see the player model despite it showing up in the game and scene tabs just fine. I tracked it down to 1 line of code, but that line of code has been in there since i started the project and has never caused any issues. Now whatever I change it to has no effect and unless i completely delete that line of code (in which case the camera no longer follows the player) im always invisible. Here is the script using System.Collections; using System.Collections.Generic; using UnityEngine; public class CameraFollow : MonoBehaviour { public GameObject target; // Update is called once per frame void Update() { //the line thats causing me to go invisible transform.position = new Vector3(target.transform.position.x, target.transform.position.y, -10); } }
2 replies
CC#
Created by mr.killmeplease on 11/18/2023 in #help
Following a video to create power ups, but its not picking them up and I dont know why.
Heres the code. I dont know whats wrong with it, from what I can tell its exactly the same as the video except that some of my scripts and variables were named differently. It should be giving a speed boost when the player touches it that lasts 10 seconds, but its not doing anything. I tried debugging it and it didnt even register me touching it. using System.Collections; using System.Collections.Generic; using UnityEngine; public class SpeedBoostPowerUp : MonoBehaviour { public float SpeedBoost = 1.5f; public GameObject pickupEffect;
//register touching power up void OnTriggerEnter2D(Collider2D other) { if (other.CompareTag("Player")) { StartCoroutine( Pickup(other) ); } } //pick up and activate power up IEnumerator Pickup(Collider2D player) { //Debug.Log("Powerup picked up!"); Instantiate(pickupEffect, transform.position, transform.rotation); PlayerMovement stats = player.GetComponent<PlayerMovement>(); stats.speed *= SpeedBoost; GetComponent<MeshRenderer>().enabled = false; GetComponent<Collider>().enabled = false; //last 10 seconds yield return new WaitForSeconds(10); stats.speed /= SpeedBoost; //destroy power up on pickup Destroy(gameObject); } }
1 replies
CC#
Created by mr.killmeplease on 11/12/2023 in #help
I Dont know what this error means and it isnt an issue the guy in the video im watching is having.
I am making a 2d platformer, and am trying to get the movement working, but I'm getting an error that doesnt show up in the video and i cant figure out why. Here is the error message: error CS1061: 'float' does not contain a definition for 'GetAxis' and no accessible extension method 'GetAxis' accepting a first argument of type 'float' could be found (are you missing a using directive or an assembly reference?) and here is the code: using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerMovement : MonoBehaviour { public float speed; private float move; private Rigidbody2D rb; // Start is called before the first frame update void Start() { rb = GetComponent<Rigidbody2D>(); } // Update is called once per frame void Update() { move.GetAxis("Horizontal"); rb.velocity = new Vector2(speed * move, rb.velocity.y); } }
6 replies
CC#
Created by mr.killmeplease on 11/5/2023 in #help
❔ How do I change the color of certain targets when they're shot, and also destroy others when shot?
I have tagged the ones to be destroyed as destroy and the ones that change color as color, but neither are working. I looked up a video for the color one, and the destroyable ones i saw that you can use Destroy(objects name) to do it, but i dont know what to write to make it destroy the target being shot and not all of them, and the color changer video just didnt work. Here is the code so far: using System.Collections; using System.Collections.Generic; using UnityEngine; public class NewBehaviourScript : MonoBehaviour { Renderer render; bool shoot; Camera cam; public float distance = 10; public LayerMask whatCanBeHit; // Start is called before the first frame update void Start() { cam = Camera.main; } // Update is called once per frame void Update() { shoot = Input.GetButton("Fire1"); } public void FixedUpdate() { if (shoot) { //Debug.Log("Shoot"); RaycastHit hit; Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, distance, whatCanBeHit); if (hit.collider.tag == "Color") { render = GetComponent<Renderer>(); render.material.color = Color.red; } else if (hit.collider.tag == "Destroy") { Destroy(gameObject); } string s = hit.collider != null ? hit.collider.name : "Nothing"; Debug.Log($"You hit {s}"); //Debug.DrawRay(cam.transform.position, cam.transform.forward * distance, Color.yellow); //Debug.DrawLine(hit.point, hit.point + hit.normal * 1, Color.red, 10f); } } }
13 replies