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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Auto Aim Error

Discussion in 'Scripting' started by MisterSixtyFour, Jun 3, 2022.

  1. MisterSixtyFour

    MisterSixtyFour

    Joined:
    Jun 28, 2021
    Posts:
    60
    So I'm working on a game, and here's a line of code I'm working on:

    Code (CSharp):
    1.  
    2.         if (scourgetimer > 0 && pos.x < 6 && pos.x > -4)
    3.         {
    4.             PointTowards(bship.transform);
    5.             PointTowards(bship.gameObject);
    6.        
    7.             scourgetimer -= Time.deltaTime;
    8.         }
    9.  
    10. void PointTowards(GameObject bship) {PointTowards(bship.transform); }
    11.     void PointTowards(Transform bship) { PointTowards(bship.position); }
    12.     void PointTowards(Vector3 bship)
    13.     {
    14.         Vector3 difference = bship - transform.position;
    15.         transform.rotation = Quaternion.LookRotation(difference.normalized, Vector3.up);
    16.     }
    Basically, this is meant to make an object find the position of the player character's gameObject, and point towards it.

    However, when I tested the script, the enemy in question just looks in a completely different angle and doesn't move.

    Is there something wrong with how I'm doing this?
     
  2. Zonlib

    Zonlib

    Joined:
    Apr 15, 2014
    Posts:
    39
    I don't see any moving code in your script.
    Try this code:
    Code (CSharp):
    1.  
    2. void PointTowards(Transform bship){
    3.      transform.LookAt(bship);
    4.      transform.position = Vector3.MoveTowards(transform.position, bship.position, speed);
    5. }
    6.  
     
  3. MisterSixtyFour

    MisterSixtyFour

    Joined:
    Jun 28, 2021
    Posts:
    60
    Ok, so I used this code, but when I started up the game, the enemy just teleported to the player character's position, and it wasn't turning at all.
    However, it is shooting bullets just fine.