Search Unity

Unity Survival Game Compile Error

Discussion in 'Scripting' started by justinmulikow664, May 19, 2019.

  1. justinmulikow664

    justinmulikow664

    Joined:
    May 19, 2019
    Posts:
    2
    I have been following the Unity 3D survival shooter game tutorial but i cannot get my script to work there is 1 error that wont let me test the game, if anyone could help me with this and fix the script would be greatly appreciated!
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour {
    6.  
    7.     public float speed = 6f;
    8.  
    9.     Vector3 movement;
    10.     Animator anim;
    11.     Rigidbody playerRigidbody;
    12.     int floorMask;
    13.     float camRayLength = 100f;
    14.  
    15.     // Use this for initialization
    16.     void Awake () {
    17.         floorMask = LayerMask.GetMask("Floor");
    18.         anim = GetComponent<Animator>();
    19.         playerRigidbody = GetComponent<Rigidbody>();
    20.     }
    21.    
    22.     // Update is called once per frame
    23.     void FixedUpdate () {
    24.         float h = Input.GetAxisRaw("Horizontal");
    25.         float v = Input.GetAxisRaw("Vertical");
    26.  
    27.         Move(h, v);
    28.         Turning();
    29.         Animating(h, v);
    30.     }
    31.  
    32.     void Move (float h, float v)
    33.     {
    34.         movement.Set(h, 0f, v);
    35.  
    36.         movement = movement.normalized * speed * Time.deltaTime;
    37.  
    38.         playerRigidbody.MovePosition(transform.position + movement);
    39.     }
    40.  
    41.     void Turning()
    42.     {
    43.         Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
    44.  
    45.         RaycastHit floorHit;
    46.  
    47.         if (Physics.Raycast(camRay, out floorHit, camRayLength, floorMask))
    48.         {
    49.             Vector3 playerToMouse.y = 0f;
    50.  
    51.             Quaternion newRotation = Quaternion.LookRotation(playerToMouse);
    52.             playerRigidbody.MoveRotation(newRotation);
    53.         }
    54.     }
    55.  
    56. void Animating(float h, float v)
    57.     {
    58.         bool walking = h != 0f || v != 0f;
    59.         anim.SetBool("IsWalking", walking);
    60.     }
    61.     }
    Assets/Unity Scripts/PlayerMovement.cs(49,33): error CS1525: Unexpected symbol `.', expecting `,', `;', or `='
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    On line 49 you have "Vector3 playerToMouse.y = 0f;"
    This is not correct syntax
    The following 2 lines are fine:
    "Vector3 playerToMouse;" This declares a vector3 called 'playerToMouse'
    "playerToMouse.y = 0f;" This sets the y value of (a previously declared value called) 'playerToMouse' to zero
    You cannot smoosh both lines together like you have done
     
    justinmulikow664 likes this.
  3. justinmulikow664

    justinmulikow664

    Joined:
    May 19, 2019
    Posts:
    2
    I followed what you said to do but the error disappears and another error comes up
    Assets/Unity Scripts/PlayerMovement.cs(52,62): error CS0165: Use of unassigned local variable `playerToMouse'
     
  4. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    You're trying to declare a local variable named "playerToMouse", but you're not giving it a value. Where is playerToMouse coming from? Did you forget to include a line of code from the tutorial that assigns a value to playerToMouse?