Search Unity

Script for Rotating to Enemy

Discussion in 'Scripting' started by sansuno68, Jun 29, 2020.

  1. sansuno68

    sansuno68

    Joined:
    Feb 10, 2020
    Posts:
    15
    Hey I'm currently working on a 3d Game and I'm working on a focus System for it, but the problem is it should happend when the enemy is close and the player click on left click and I tried with a lerp to make it smooth but it wont make the whole Transition on one click so do u have another way for me to solve it cause on one click the player should slerp rotate and look to the enemy.

    Thx for ur Help
    Code (CSharp):
    1. if (Input.GetKeyDown(KeyCode.Mouse0))
    2.         {
    3.             transform.rotation = Quaternion.Slerp(transform.rotation, (Quaternion.LookRotation(EnemiePos - transform.position)), Time.deltaTime * 10f);
    4.         }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    If you want the rotation to take more than one frame, you will need to adjust your mindset. The code you have here only runs for the exact frame that Mouse0 is pressed. So we have an action that we want to take multiple frames, but our code to process that action is only set up to run for a single frame. Something is amiss here!

    Instead of trying to do your rotation inside this if statement for Input.GetKeyDown(KeyCode.Mouse0)), use that opportunity to set up a rotation that is going to happen over multiple frames! You can do this by saving a few key pieces of information to some instance variables:
    • the rotation at the start (Quaternion)
    • the rotation you are trying to move to (Quaternion)
    • the time at which the rotation started (float)
    • the fact that you are now performing a rotation (bool)
    With those 4 pieces of information safely stored in a variable, you can do your Slerp in your update function outside of the Input detection statement by having another if statement that checks if we're currently performing a rotation. if we are, we can Slerp the current rotation of our object just a little bit each frame. When we're done rotating, we simply set that we are no longer rotating, and we're done.
     
  3. Zer0Cool

    Zer0Cool

    Joined:
    Oct 24, 2014
    Posts:
    203
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RotatetoEnemy : MonoBehaviour
    6. {
    7.     public Transform Enemy;
    8.     private bool isRotating = false;
    9.    
    10.     // Update is called once per frame
    11.     void Update()
    12.     {
    13.         if (Input.GetKeyDown(KeyCode.Mouse0) && !isRotating)
    14.         {
    15.             StartCoroutine(RotateToEnemy(1f));          
    16.         }
    17.     }
    18.  
    19.     IEnumerator RotateToEnemy(float duration)
    20.     {
    21.         isRotating = true;
    22.         Quaternion startRotation = transform.rotation;
    23.         Quaternion endRotation = Quaternion.LookRotation(Enemy.position - transform.position);
    24.         float t = 0.0f;
    25.         while (t < duration)
    26.         {
    27.             t += Time.deltaTime;
    28.             transform.rotation = Quaternion.Slerp(startRotation, endRotation, t / duration);          
    29.             yield return null;
    30.         }
    31.         isRotating = false;
    32.     }
    33. }
    34.