Character Camera does not move!

4

I'm doing a Unity tutorial that I downloaded to the Asset Store at the following link link

I tried to make my character turn to look at the mouse, but it can not rotate, and just look forward to the case and I can not make it look at other places, like for example the right, left, back of the character, only in a straight line! I use the Turning() function to take care of 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);
    }
}

Player Inspector - link

FloorInspector- link

    
asked by anonymous 11.03.2015 / 16:13

1 answer

7

The problem is with your Floor (Quad) object.

How much do you use a Raycast coming out of the camera at

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

You pass as parameter floorMask

What is instantiated in

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

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

IfitdoesnotfindanyobjectwiththeLayermarkedFlooritwillnotenteryourifandwillnotperformthecodethatmakesthecharacterrotate.

WhenyouseethescreenshotoftheInspectorofyourfloor,itismarkedasDefault,youcanchangeitto"Floor" - If there is not, just create one. What to solve

So every time you work with Layers (or Tags) in code in Unity, it's worth checking if the objects are configured correctly, or if the name is the same (uppercase and lowercase are differentiated). And when using a Raycast, also check that the coordinates are passing through the desired points. Sometimes it can not touch any object with the Layer checked, so it is good to check if the origin is right (in this case the Camera) and if the direction is correct (picks up from the mouse).

    
12.03.2015 / 02:42