Search Unity

I need an advice.

Discussion in 'Getting Started' started by PetarD05, Jan 10, 2018.

  1. PetarD05

    PetarD05

    Joined:
    Dec 29, 2017
    Posts:
    8
    Hi guys, I am a beginer in Unity, and I need your help. How do I get a FirstPersonController to collect an object and count it?
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,521
  3. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    The tutorials cover this kind of thing. Specifically, Roll-A-Ball has pickups that work the way you're describing.
     
    Kiwasi likes this.
  4. PetarD05

    PetarD05

    Joined:
    Dec 29, 2017
    Posts:
    8
    But I have did from rool a ball totorial and it doesnt work
     
  5. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,521
    Try harder?
     
    hippocoder likes this.
  6. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    "It doesn't work" is literally the least useful piece of information you could provide when asking for assistance.

    What, specifically, is not working?

    What have you tried? (Share your code!) (Use code tags!)

    What errors, if any, are you getting?

    Share some screenshots or video of what you're getting and describe how that's different than what you're expecting.
     
    hippocoder likes this.
  7. PetarD05

    PetarD05

    Joined:
    Dec 29, 2017
    Posts:
    8
    here,can you tell me why it isnt working? upload_2018-1-11_19-22-30.png
     

    Attached Files:

  8. PetarD05

    PetarD05

    Joined:
    Dec 29, 2017
    Posts:
    8
  9. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Well for starters, you have 6 errors in your console. Those have to be resolved before you can even run the game. My first guess would be you're missing some semicolons at the end of a few lines.
     
    jhocking and Kiwasi like this.
  10. PetarD05

    PetarD05

    Joined:
    Dec 29, 2017
    Posts:
    8
    upload_2018-1-12_11-13-25.png upload_2018-1-12_11-13-39.png
     
  11. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    @PetarD05 Since you didn't actually post a question, I'm assuming you're having trouble figuring out how to fix that last error. Let me walk you through it.

    "Unexpected symbol" means the compiler encountered something that surprised it. So let's say in my made up language, all sentences must be structured like "[NOUN] [VERB] [ADVERB]." If I try typing out:
    The compiler will fail at "home" because it expects to find an adverb and instead is encountering a noun. In your case, the compiler is telling you it encountered the word "void" when it didn't expect to see it.

    'void' is a keyword that indicated you're declaring a method with no return type. Methods can only be declared at the root level of classes; not inside other methods, and not outside of a class. So that means if the compiler has an issue with the 'void' declaration, you're probably violating one of those rules. (Alternatively, it could still be because of a missing semicolon, but that's not the case here.)

    You didn't paste your whole script (sharing code with code tags is better than posting screenshots), so I can only assume, but things don't look correct to me right before your OnTriggerEnter method. It looks like there's one too many closing curly brace. You should go through and see how they line up and make sure your method is being declared in the correct place and that you're not declaring it outside the class definition.

    I can also see that your code isn't formatted correctly as far as indent levels go. While indenting is all optional, formatting your code correctly helps make these errors more obvious.
     
    jhocking, Ryiah and hippocoder like this.
  12. PetarD05

    PetarD05

    Joined:
    Dec 29, 2017
    Posts:
    8
    Here
    The whole script



    using System;
    using UnityEngine;
    using UnityStandardAssets.CrossPlatformInput;
    using UnityStandardAssets.Utility;
    using Random = UnityEngine.Random;

    namespace UnityStandardAssets.Characters.FirstPerson
    {
    [RequireComponent(typeof (CharacterController))]
    [RequireComponent(typeof (AudioSource))]
    public class FirstPersonController : MonoBehaviour
    {
    [SerializeField] private bool m_IsWalking;
    [SerializeField] private float m_WalkSpeed;
    [SerializeField] private float m_RunSpeed;
    [SerializeField] [Range(0f, 1f)] private float m_RunstepLenghten;
    [SerializeField] private float m_JumpSpeed;
    [SerializeField] private float m_StickToGroundForce;
    [SerializeField] private float m_GravityMultiplier;
    [SerializeField] private MouseLook m_MouseLook;
    [SerializeField] private bool m_UseFovKick;
    [SerializeField] private FOVKick m_FovKick = new FOVKick();
    [SerializeField] private bool m_UseHeadBob;
    [SerializeField] private CurveControlledBob m_HeadBob = new CurveControlledBob();
    [SerializeField] private LerpControlledBob m_JumpBob = new LerpControlledBob();
    [SerializeField] private float m_StepInterval;
    [SerializeField] private AudioClip[] m_FootstepSounds; // an array of footstep sounds that will be randomly selected from.
    [SerializeField] private AudioClip m_JumpSound; // the sound played when character leaves the ground.
    [SerializeField] private AudioClip m_LandSound; // the sound played when character touches back on ground.

    private Camera m_Camera;
    private bool m_Jump;
    private float m_YRotation;
    private Vector2 m_Input;
    private Vector3 m_MoveDir = Vector3.zero;
    private CharacterController m_CharacterController;
    private CollisionFlags m_CollisionFlags;
    private bool m_PreviouslyGrounded;
    private Vector3 m_OriginalCameraPosition;
    private float m_StepCycle;
    private float m_NextStep;
    private bool m_Jumping;
    private AudioSource m_AudioSource;

    // Use this for initialization
    private void Start()
    {
    m_CharacterController = GetComponent<CharacterController>();
    m_Camera = Camera.main;
    m_OriginalCameraPosition = m_Camera.transform.localPosition;
    m_FovKick.Setup(m_Camera);
    m_HeadBob.Setup(m_Camera, m_StepInterval);
    m_StepCycle = 0f;
    m_NextStep = m_StepCycle/2f;
    m_Jumping = false;
    m_AudioSource = GetComponent<AudioSource>();
    m_MouseLook.Init(transform , m_Camera.transform);
    }


    // Update is called once per frame
    private void Update()
    {
    RotateView();
    // the jump state needs to read here to make sure it is not missed
    if (!m_Jump)
    {
    m_Jump = CrossPlatformInputManager.GetButtonDown("Jump");
    }

    if (!m_PreviouslyGrounded && m_CharacterController.isGrounded)
    {
    StartCoroutine(m_JumpBob.DoBobCycle());
    PlayLandingSound();
    m_MoveDir.y = 0f;
    m_Jumping = false;
    }
    if (!m_CharacterController.isGrounded && !m_Jumping && m_PreviouslyGrounded)
    {
    m_MoveDir.y = 0f;
    }

    m_PreviouslyGrounded = m_CharacterController.isGrounded;
    }


    private void PlayLandingSound()
    {
    m_AudioSource.clip = m_LandSound;
    m_AudioSource.Play();
    m_NextStep = m_StepCycle + .5f;
    }


    private void FixedUpdate()
    {
    float speed;
    GetInput(out speed);
    // always move along the camera forward as it is the direction that it being aimed at
    Vector3 desiredMove = transform.forward*m_Input.y + transform.right*m_Input.x;

    // get a normal for the surface that is being touched to move along it
    RaycastHit hitInfo;
    Physics.SphereCast(transform.position, m_CharacterController.radius, Vector3.down, out hitInfo,
    m_CharacterController.height/2f, Physics.AllLayers, QueryTriggerInteraction.Ignore);
    desiredMove = Vector3.ProjectOnPlane(desiredMove, hitInfo.normal).normalized;

    m_MoveDir.x = desiredMove.x*speed;
    m_MoveDir.z = desiredMove.z*speed;


    if (m_CharacterController.isGrounded)
    {
    m_MoveDir.y = -m_StickToGroundForce;

    if (m_Jump)
    {
    m_MoveDir.y = m_JumpSpeed;
    PlayJumpSound();
    m_Jump = false;
    m_Jumping = true;
    }
    }
    else
    {
    m_MoveDir += Physics.gravity*m_GravityMultiplier*Time.fixedDeltaTime;
    }
    m_CollisionFlags = m_CharacterController.Move(m_MoveDir*Time.fixedDeltaTime);

    ProgressStepCycle(speed);
    UpdateCameraPosition(speed);

    m_MouseLook.UpdateCursorLock();
    }


    private void PlayJumpSound()
    {
    m_AudioSource.clip = m_JumpSound;
    m_AudioSource.Play();
    }


    private void ProgressStepCycle(float speed)
    {
    if (m_CharacterController.velocity.sqrMagnitude > 0 && (m_Input.x != 0 || m_Input.y != 0))
    {
    m_StepCycle += (m_CharacterController.velocity.magnitude + (speed*(m_IsWalking ? 1f : m_RunstepLenghten)))*
    Time.fixedDeltaTime;
    }

    if (!(m_StepCycle > m_NextStep))
    {
    return;
    }

    m_NextStep = m_StepCycle + m_StepInterval;

    PlayFootStepAudio();
    }


    private void PlayFootStepAudio()
    {
    if (!m_CharacterController.isGrounded)
    {
    return;
    }
    // pick & play a random footstep sound from the array,
    // excluding sound at index 0
    int n = Random.Range(1, m_FootstepSounds.Length);
    m_AudioSource.clip = m_FootstepSounds[n];
    m_AudioSource.PlayOneShot(m_AudioSource.clip);
    // move picked sound to index 0 so it's not picked next time
    m_FootstepSounds[n] = m_FootstepSounds[0];
    m_FootstepSounds[0] = m_AudioSource.clip;
    }


    private void UpdateCameraPosition(float speed)
    {
    Vector3 newCameraPosition;
    if (!m_UseHeadBob)
    {
    return;
    }
    if (m_CharacterController.velocity.magnitude > 0 && m_CharacterController.isGrounded)
    {
    m_Camera.transform.localPosition =
    m_HeadBob.DoHeadBob(m_CharacterController.velocity.magnitude +
    (speed*(m_IsWalking ? 1f : m_RunstepLenghten)));
    newCameraPosition = m_Camera.transform.localPosition;
    newCameraPosition.y = m_Camera.transform.localPosition.y - m_JumpBob.Offset();
    }
    else
    {
    newCameraPosition = m_Camera.transform.localPosition;
    newCameraPosition.y = m_OriginalCameraPosition.y - m_JumpBob.Offset();
    }
    m_Camera.transform.localPosition = newCameraPosition;
    }


    private void GetInput(out float speed)
    {
    // Read input
    float horizontal = CrossPlatformInputManager.GetAxis("Horizontal");
    float vertical = CrossPlatformInputManager.GetAxis("Vertical");

    bool waswalking = m_IsWalking;

    #if !MOBILE_INPUT
    // On standalone builds, walk/run speed is modified by a key press.
    // keep track of whether or not the character is walking or running
    m_IsWalking = !Input.GetKey(KeyCode.LeftShift);
    #endif
    // set the desired speed to be walking or running
    speed = m_IsWalking ? m_WalkSpeed : m_RunSpeed;
    m_Input = new Vector2(horizontal, vertical);

    // normalize input if it exceeds 1 in combined length:
    if (m_Input.sqrMagnitude > 1)
    {
    m_Input.Normalize();
    }

    // handle speed change to give an fov kick
    // only if the player is going to a run, is running and the fovkick is to be used
    if (m_IsWalking != waswalking && m_UseFovKick && m_CharacterController.velocity.sqrMagnitude > 0)
    {
    StopAllCoroutines();
    StartCoroutine(!m_IsWalking ? m_FovKick.FOVKickUp() : m_FovKick.FOVKickDown());
    }
    }


    private void RotateView()
    {
    m_MouseLook.LookRotation (transform, m_Camera.transform);
    }


    private void OnControllerColliderHit(ControllerColliderHit hit)
    {
    Rigidbody body = hit.collider.attachedRigidbody;
    //dont move the rigidbody if the character is on top of it
    if (m_CollisionFlags == CollisionFlags.Below)
    {
    return;
    }

    if (body == null || body.isKinematic)
    {
    return;
    }
    body.AddForceAtPosition(m_CharacterController.velocity*0.1f, hit.point, ForceMode.Impulse);
    }
    }
    }
    {

    void OnTriggerEnter(Collider other)
    {
    if (other.gameObject.CompareTag("Coin"))
    {
    other.gameObject.SetActive (false);
    }
    }
    }
     
  13. PetarD05

    PetarD05

    Joined:
    Dec 29, 2017
    Posts:
    8
    upload_2018-1-13_15-45-58.png
    using System;
    using UnityEngine;
    using UnityStandardAssets.CrossPlatformInput;
    using UnityStandardAssets.Utility;
    using Random = UnityEngine.Random;

    namespace UnityStandardAssets.Characters.FirstPerson
    {
    [RequireComponent(typeof (CharacterController))]
    [RequireComponent(typeof (AudioSource))]
    public class FirstPersonController : MonoBehaviour
    {
    [SerializeField] private bool m_IsWalking;
    [SerializeField] private float m_WalkSpeed;
    [SerializeField] private float m_RunSpeed;
    [SerializeField] [Range(0f, 1f)] private float m_RunstepLenghten;
    [SerializeField] private float m_JumpSpeed;
    [SerializeField] private float m_StickToGroundForce;
    [SerializeField] private float m_GravityMultiplier;
    [SerializeField] private MouseLook m_MouseLook;
    [SerializeField] private bool m_UseFovKick;
    [SerializeField] private FOVKick m_FovKick = new FOVKick();
    [SerializeField] private bool m_UseHeadBob;
    [SerializeField] private CurveControlledBob m_HeadBob = new CurveControlledBob();
    [SerializeField] private LerpControlledBob m_JumpBob = new LerpControlledBob();
    [SerializeField] private float m_StepInterval;
    [SerializeField] private AudioClip[] m_FootstepSounds; // an array of footstep sounds that will be randomly selected from.
    [SerializeField] private AudioClip m_JumpSound; // the sound played when character leaves the ground.
    [SerializeField] private AudioClip m_LandSound; // the sound played when character touches back on ground.

    private Camera m_Camera;
    private bool m_Jump;
    private float m_YRotation;
    private Vector2 m_Input;
    private Vector3 m_MoveDir = Vector3.zero;
    private CharacterController m_CharacterController;
    private CollisionFlags m_CollisionFlags;
    private bool m_PreviouslyGrounded;
    private Vector3 m_OriginalCameraPosition;
    private float m_StepCycle;
    private float m_NextStep;
    private bool m_Jumping;
    private AudioSource m_AudioSource;

    // Use this for initialization
    private void Start()
    {
    m_CharacterController = GetComponent<CharacterController>();
    m_Camera = Camera.main;
    m_OriginalCameraPosition = m_Camera.transform.localPosition;
    m_FovKick.Setup(m_Camera);
    m_HeadBob.Setup(m_Camera, m_StepInterval);
    m_StepCycle = 0f;
    m_NextStep = m_StepCycle/2f;
    m_Jumping = false;
    m_AudioSource = GetComponent<AudioSource>();
    m_MouseLook.Init(transform , m_Camera.transform);
    }


    // Update is called once per frame
    private void Update()
    {
    RotateView();
    // the jump state needs to read here to make sure it is not missed
    if (!m_Jump)
    {
    m_Jump = CrossPlatformInputManager.GetButtonDown("Jump");
    }

    if (!m_PreviouslyGrounded && m_CharacterController.isGrounded)
    {
    StartCoroutine(m_JumpBob.DoBobCycle());
    PlayLandingSound();
    m_MoveDir.y = 0f;
    m_Jumping = false;
    }
    if (!m_CharacterController.isGrounded && !m_Jumping && m_PreviouslyGrounded)
    {
    m_MoveDir.y = 0f;
    }

    m_PreviouslyGrounded = m_CharacterController.isGrounded;
    }


    private void PlayLandingSound()
    {
    m_AudioSource.clip = m_LandSound;
    m_AudioSource.Play();
    m_NextStep = m_StepCycle + .5f;
    }


    private void FixedUpdate()
    {
    float speed;
    GetInput(out speed);
    // always move along the camera forward as it is the direction that it being aimed at
    Vector3 desiredMove = transform.forward*m_Input.y + transform.right*m_Input.x;

    // get a normal for the surface that is being touched to move along it
    RaycastHit hitInfo;
    Physics.SphereCast(transform.position, m_CharacterController.radius, Vector3.down, out hitInfo,
    m_CharacterController.height/2f, Physics.AllLayers, QueryTriggerInteraction.Ignore);
    desiredMove = Vector3.ProjectOnPlane(desiredMove, hitInfo.normal).normalized;

    m_MoveDir.x = desiredMove.x*speed;
    m_MoveDir.z = desiredMove.z*speed;


    if (m_CharacterController.isGrounded)
    {
    m_MoveDir.y = -m_StickToGroundForce;

    if (m_Jump)
    {
    m_MoveDir.y = m_JumpSpeed;
    PlayJumpSound();
    m_Jump = false;
    m_Jumping = true;
    }
    }
    else
    {
    m_MoveDir += Physics.gravity*m_GravityMultiplier*Time.fixedDeltaTime;
    }
    m_CollisionFlags = m_CharacterController.Move(m_MoveDir*Time.fixedDeltaTime);

    ProgressStepCycle(speed);
    UpdateCameraPosition(speed);

    m_MouseLook.UpdateCursorLock();
    }


    private void PlayJumpSound()
    {
    m_AudioSource.clip = m_JumpSound;
    m_AudioSource.Play();
    }


    private void ProgressStepCycle(float speed)
    {
    if (m_CharacterController.velocity.sqrMagnitude > 0 && (m_Input.x != 0 || m_Input.y != 0))
    {
    m_StepCycle += (m_CharacterController.velocity.magnitude + (speed*(m_IsWalking ? 1f : m_RunstepLenghten)))*
    Time.fixedDeltaTime;
    }

    if (!(m_StepCycle > m_NextStep))
    {
    return;
    }

    m_NextStep = m_StepCycle + m_StepInterval;

    PlayFootStepAudio();
    }


    private void PlayFootStepAudio()
    {
    if (!m_CharacterController.isGrounded)
    {
    return;
    }
    // pick & play a random footstep sound from the array,
    // excluding sound at index 0
    int n = Random.Range(1, m_FootstepSounds.Length);
    m_AudioSource.clip = m_FootstepSounds[n];
    m_AudioSource.PlayOneShot(m_AudioSource.clip);
    // move picked sound to index 0 so it's not picked next time
    m_FootstepSounds[n] = m_FootstepSounds[0];
    m_FootstepSounds[0] = m_AudioSource.clip;
    }


    private void UpdateCameraPosition(float speed)
    {
    Vector3 newCameraPosition;
    if (!m_UseHeadBob)
    {
    return;
    }
    if (m_CharacterController.velocity.magnitude > 0 && m_CharacterController.isGrounded)
    {
    m_Camera.transform.localPosition =
    m_HeadBob.DoHeadBob(m_CharacterController.velocity.magnitude +
    (speed*(m_IsWalking ? 1f : m_RunstepLenghten)));
    newCameraPosition = m_Camera.transform.localPosition;
    newCameraPosition.y = m_Camera.transform.localPosition.y - m_JumpBob.Offset();
    }
    else
    {
    newCameraPosition = m_Camera.transform.localPosition;
    newCameraPosition.y = m_OriginalCameraPosition.y - m_JumpBob.Offset();
    }
    m_Camera.transform.localPosition = newCameraPosition;
    }


    private void GetInput(out float speed)
    {
    // Read input
    float horizontal = CrossPlatformInputManager.GetAxis("Horizontal");
    float vertical = CrossPlatformInputManager.GetAxis("Vertical");

    bool waswalking = m_IsWalking;

    #if !MOBILE_INPUT
    // On standalone builds, walk/run speed is modified by a key press.
    // keep track of whether or not the character is walking or running
    m_IsWalking = !Input.GetKey(KeyCode.LeftShift);
    #endif
    // set the desired speed to be walking or running
    speed = m_IsWalking ? m_WalkSpeed : m_RunSpeed;
    m_Input = new Vector2(horizontal, vertical);

    // normalize input if it exceeds 1 in combined length:
    if (m_Input.sqrMagnitude > 1)
    {
    m_Input.Normalize();
    }

    // handle speed change to give an fov kick
    // only if the player is going to a run, is running and the fovkick is to be used
    if (m_IsWalking != waswalking && m_UseFovKick && m_CharacterController.velocity.sqrMagnitude > 0)
    {
    StopAllCoroutines();
    StartCoroutine(!m_IsWalking ? m_FovKick.FOVKickUp() : m_FovKick.FOVKickDown());
    }
    }


    private void RotateView()
    {
    m_MouseLook.LookRotation (transform, m_Camera.transform);
    }


    private void OnControllerColliderHit(ControllerColliderHit hit)
    {
    Rigidbody body = hit.collider.attachedRigidbody;
    //dont move the rigidbody if the character is on top of it
    if (m_CollisionFlags == CollisionFlags.Below)
    {
    return;
    }

    if (body == null || body.isKinematic)
    {
    return;
    }
    body.AddForceAtPosition(m_CharacterController.velocity*0.1f, hit.point, ForceMode.Impulse);
    }

    void OnTriggerEnter(Collider other)
    {
    if (other.gameObject.CompareTag ("Coin"))
    {
    other.gameObject.SetActive (false);
    }
    }
    }
    upload_2018-1-13_15-46-58.png
     
  14. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You didn't use code tags and that's a huge long post :)

    One suggestion might be to just replace the First person script with a new copy and then add back in what you were doing piece by piece, making sure to check that the syntax is valid.
    Then, if you're still stuck later (without compiler errors), please post again showing your (relevant only!) code :)

    Hope that helps.
     
  15. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Unexpected symbol is almost always to do with what happens immediately before the listed symbol. Normally a missed semicolon, although missing anything will trigger it.
     
    jhocking likes this.
  16. PetarD05

    PetarD05

    Joined:
    Dec 29, 2017
    Posts:
    8
    thank you I solved the problem
     
  17. BIGTIMEMASTER

    BIGTIMEMASTER

    Joined:
    Jun 1, 2017
    Posts:
    5,181
    The thread title asked for advice, but what you actually asked for was a complex problem to be solved.

    So here is advice : When you are new to programming, 99% of the problems you will face is because you missed a semicolon somewhere. Get used to going through code, letter by letter.
     
  18. Bill_Martini

    Bill_Martini

    Joined:
    Apr 19, 2016
    Posts:
    445
    Yeah, missing semicolons or missing/extra curly brackets are always the issue and new comers tend to overlook syntax. The compiler needs you to be correct in your syntax or it looses it's mind.

    Please use code tags when posting code. The unformatted view is very hard to read and many simply will not bother to try to find a problem in unformatted code. You'll get faster and better responses when you help others help you.