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

Stop Character simplemove, whenn hitting certain gametag

Discussion in 'Scripting' started by meierdesigns_unity, Apr 19, 2020.

  1. meierdesigns_unity

    meierdesigns_unity

    Joined:
    Apr 16, 2020
    Posts:
    7
    Hey guys,

    I am following a tutorial, where they explain how to create a diablo like movement.

    I have the following code:

    Code (CSharp):
    1.     void MoveToPosition()
    2.     {
    3.         if (Vector3.Distance(transform.position, position) > 1 && !GetComponent<Animation>().IsPlaying("attack"))
    4.         {
    5.  
    6.             Quaternion newRotation = Quaternion.LookRotation(position - transform.position);
    7.  
    8.             newRotation.x = 0f;
    9.             newRotation.z = 0f;
    10.  
    11.             transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 10);
    12.             controller.SimpleMove(transform.forward * speed);
    13.  
    14.             GetComponent<Animation>().CrossFade("run");
    15.  
    16.         }
    17.  
    18.         else
    19.         {
    20.             GetComponent<Animation>().CrossFade("idle");
    21.         }
    22.     }
    what would be within an if statement and where would you put it?

    Thanks for the help, people!
     
  2. meierdesigns_unity

    meierdesigns_unity

    Joined:
    Apr 16, 2020
    Posts:
    7
    void OnControllerColliderHit(ControllerColliderHit hit)
    {
    if (hit.collider.tag == "Walls" || hit.collider.tag == "Enemy")
    {
    position = new Vector3(hit.point.x, hit.point.y, hit.point.z);
    GetComponent<Animation>().Stop("run");
    GetComponent<Animation>().Play("idle");
    }
    }

    Here's my solution. I set the variable position for the moveToPosition function to the point that got hit when hitting the wall. I could also say hit.collider.tag != "Walls" but maybe there are some effects that I want to be able to run through in the future.