Character Camera does not move!

Asked

Viewed 369 times

4

I’m doing a tutorial on Unity that I downloaded in the Asset Store on the following link https://www.assetstore.unity3d.com/en/#! /content/21028

I tried to make my character rotate to look towards the mouse, but he can’t rotate, and only looks forward in the case and I can’t make him look at the other places, such as the right corner, left, behind the character, only in a straight line! Use the function Turning() to take care of the rotation.

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 FixedUpdate(){
        float h = Input.GetAxisRaw ("Horizontal");
        float v = Input.GetAxisRaw ("Vertical");
        Move (h, v);
        Turning ();
        Animating (h, v);
    }

    void Move(float h,float v){
        //tertemina o movimento
        movement.Set(h,0f,v);
        //normaliza o movimento
        movement = movement.normalized * speed * Time.deltaTime;
        // efetua o movimento
        playerRigidbody.MovePosition (transform.position + movement);
    }

    //girar o jogador
    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;
            // rotaçao do personagem
            Quaternion newRotation= Quaternion.LookRotation(playerToMouse);
            playerRigidbody.MoveRotation(newRotation);
        }
    }

    void Animating(float h,float v){
        bool walking = h != 0f || v!=0f;
        anim.SetBool ("IsWalking", walking);
    }
}

Inspector do Player - http://i.imgur.com/p6BgvJw.png Inspector do Player

Floor Inspector - http://i.imgur.com/Pgwlkxt.png Inspector do Floor

  • 1

    @Derpplayss, is the camera working right? Changing the Floor solved everything? Is there any more problem?

  • 3

    @Nils Feel free to post an answer now :) Outstanding its initiative to encourage its members to come here and ask questions. Thank you for sharing the site with them.

  • 1

    @Qmechanic73 Thanks to stackoverflow and its community, I learned a lot coming here and the structure to solve doubts collectively is sensational. And a few weeks ago I saw that now you can doubt almost everything(has area for everything).

1 answer

7

The problem is with your Floor object(Quad).

When you use a Raycast coming out of the camera on

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

You pass as parameter the floorMask

Which is instantiated in

void Awake(){
    floorMask = LayerMask.GetMask ("Floor");
    ..
}

That means that the radius coming out of the camera will look for a Layer called "Floor", more or less like this diagram.

Diagrama do Raycast da câmera

If it does not find any object with Layer marked as Floor it will not enter your ifand will not perform the code that makes the character spin.

You when I saw the Inspector screenshot of your floor, it is marked as Default, you can change it to "Floor" - If there is not, just create one. That should solve

Layer_Floor

Therefore, every time you work with Layers(or Tags) in code in Unity, it is worth checking if the objects are configured correctly, or if the name is the same(case and minuscule are differentiated). And when using a Raycast, also check that the coordinates are passing through the desired points. Because sometimes it may not touch any object with the Layer marked, so it is good to check if the source is right (in this case the Camera) and if the direction is correct (take from the mouse).

Browser other questions tagged

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