Collision recognition

Asked

Viewed 136 times

1

inserir a descrição da imagem aquiI am developing through tutorials the game Unity training day (Nightmares) and I realized that the player did not recognize collisions in objects.

When adding the "Capsule Collider" component, the error has been fixed, but the character moves alone. Where is the error?

inserir a descrição da imagem aqui`

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public float speed = 6f;

    Vector3 movement;
    Animator anim;
    Rigidbody playerRigidbody;
    int floorMask;
    float camRayLenght = 100f;

    void Awake ()
    {
        floorMask = LayerMask.GetMask ("Floor");
        anim = GetComponent <Animator> ();
        playerRigidbody = GetComponent <Rigidbody> ();

    }


    void Update ()
    {
        float h =Input.GetAxisRaw("Horizontal");
        float v =Input.GetAxisRaw("Vertical");

        Move (h,v);
        Turning ();
        Animating (h, v);

    }


    void Move (float h, float v)
    {

        movement.Set (h, 0f, v);

        movement = movement.normalized * speed * Time.deltaTime  ;

        playerRigidbody.MovePosition (transform.position + movement);

    }

    void Turning ()
    {

        Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);

        RaycastHit floorHit;

        if(Physics.Raycast (camRay, out floorHit, camRayLenght, floorMask))
        {

            Vector3 playerToMouse = floorHit.point - transform.position;

            playerToMouse.y = 0f;

            Quaternion newRotation = Quaternion.LookRotation (playerToMouse);

            playerRigidbody.MoveRotation (newRotation);
        }




    }


    void Animating (float h,float v)
    {

        bool walking = h != 0f || v !=0f;

        anim.SetBool ("IsWalking", walking);
    }
}

<code>inserir a descrição da imagem aqui</code> Capsule Collider

  • Hello @pedrofarah, could send more information about the player, please". Moving alone is not something unique to Collider, if possible send the capture of the full player Inspector, player code, and see if there is any other script/component that affects the player indirectly, depending on the part of the tutorial you have the attacks he suffers, and Ud. (but I think only in inspector, and player code we will find the solution.

  • Information Added Here!

  • Everything seems in order, I’ll ask you for some favors. 1 - What is written on the Tag and Player Layer? 2 - Have a v on the Capsule Collider side, click on it to uncheck see if it stopped walking. 3 - Send the floor Ispector (inside Environment). 4 - You even did the Nav mesh step?

  • Nils ,in the tag is "Player" and in Layer is "Default". If v is unchecked the player stops walking alone but the collisions are not recognized and I have not yet reached that step.. added some more information

  • Very strange this, was not to happen this problem if you did the same as the video. And the Transform of your player? Make sure he’s not crawling under the floor. And tidy up your question, it’s a mess, and with duplicate images.

  • 1

    By the way, is moving yourself walking around on the map, or playing the animation you’re walking around with? Do you have any controls plugged into your pc? Beyond the keyboard and mouse?

  • 1

    Peter, this question is very difficult to answer because, again, you do not detail your problem in a clear way. Perhaps because as you are still learning you do not understand what happens in the right way. See, your headline says nothing (it just says "collision recognition"). It then says that this problem (from the title) has been fixed, and that the problem is that the character "moves alone". As colleague @Nils has already mentioned, what is "moves alone"? The object is translated by the screen (literally moves), or the animation of the floor is executed and it stands still?

  • 1

    Don’t take this the wrong way, but since you’re following a quality video, the problem is probably (again) a lack of attention from you in some aspect. Despite the good will, this site is not a forum and we can not experience all possible combinations with you. Try to be clearer on the problem (and without repeating images, as already suggested). Also, try to review the video and redo your steps (checking the settings/code), paying attention to the details.

  • 1

    Besides, it’s a little frustrating to insist on helping you find out it was a mere lack of attention. Moreover, this does not contribute to the quality of the site’s content, as the problems tend not to serve to help other people. Ah, and also does not collaborate for your learning, because it is the process of trial and error occurs almost exclusively on the part of others (who already know) and not of who should be really interested (you, who still does not know so much). :)

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.