Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Unit: Point on object instead of point on mouse?

Discussion in 'Scripting' started by dznrandom, May 11, 2022.

  1. dznrandom

    dznrandom

    Joined:
    Oct 3, 2019
    Posts:
    2
    Hey,
    I would like to add an Aimbot to my Unity Game - so that the ragdoll aims at the enemy instead of the mouse position.
    Thanks for the help! Enemy is currently a box with box collider and rigidbody.
    As you can see in the script below, the arms only aim at the mouse position when the left mouse button is pressed. What I want though: If the left mouse button is pressed once for a short time, it aims at the enemy, shoots and then lets the arm hang again normally.
    Here is the aim script which is on the arm of the ragdoll (the aim should be a bit random would regulate the angle limit):

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. public class Arms : MonoBehaviour
    7. {
    8.     [Range(0, 1)] public int isLeftOrRight;
    9.     public float speed = 300f;
    10.     public Camera cam;
    11.  
    12.  
    13.     private Rigidbody2D rb;
    14.  
    15.  
    16.     private void Start()
    17.     {
    18.         rb = GetComponent<Rigidbody2D>();
    19.     }
    20.  
    21.  
    22.     private void Update()
    23.     {
    24.         Vector3 mousePos = new Vector3(cam.ScreenToWorldPoint(Input.mousePosition).x, cam.ScreenToWorldPoint(Input.mousePosition).y, 0f);
    25.         Vector3 difference = mousePos - transform.position;
    26.         float rotationZ = Mathf.Atan2(difference.x, -difference.y) * Mathf.Rad2Deg -90;
    27.  
    28.  
    29.         if (Input.GetMouseButton(isLeftOrRight))
    30.         {
    31.             rb.MoveRotation(Mathf.LerpAngle(rb.rotation, rotationZ, speed * Time.deltaTime));
    32.         }
    33.     }
    34. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,963
    Sounds like a good application for some kind of cooldown timer.

    Cooldown timers, gun bullet intervals, shot spacing, rate of fire:

    https://forum.unity.com/threads/fire-rate-issues.1026154/#post-6646297

    GunHeat (gunheat) spawning shooting rate of fire:

    https://forum.unity.com/threads/spawning-after-amount-of-time-without-spamming.1039618/#post-6729841

    As you implement it, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
  3. dznrandom

    dznrandom

    Joined:
    Oct 3, 2019
    Posts:
    2
    Thanks for ur time but that wasn't my question. :c
    I just wanted to know if there is an easy and quick way to aim at the enemy (game object) instead at the mouse position when i left klick.

    Thanks!