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. Dismiss Notice

I've run into a problem.

Discussion in 'Scripting' started by Kalamity1, Feb 7, 2015.

  1. Kalamity1

    Kalamity1

    Joined:
    Feb 7, 2015
    Posts:
    2
    Posted below is my script for the beginner survival shooter. It would appear that the movement controls work fine, but the mouse controls don't(being able to turn character). If anyone can point out what is wrong as soon as possible, it would be much appreciated. Thanks.



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

    Vector3 movement;
    Animator anim;
    Rigidbody playerRigidbody;
    int floorMask;
    float camRayLength = 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)
    {
    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, camRayLength, 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);
    }
    }
     
  2. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    Repost with code tags, please
     
    tango209 likes this.
  3. tango209

    tango209

    Joined:
    Feb 23, 2011
    Posts:
    379
    Have you tried debugging your script? If not, please take a look at the link in my signature on how to go about it. If you still have problems please repost with Code Tags as BenZed has suggested. See the other link in my signature if you need help with code tags.
     
  4. Kalamity1

    Kalamity1

    Joined:
    Feb 7, 2015
    Posts:
    2
    where do I find code tags? im new to this.
     
  5. tango209

    tango209

    Joined:
    Feb 23, 2011
    Posts:
    379
    Click the How to use Code Tags link in my signature
    |
    |
    V