locked
My Unity tutorial game code has only two bugs but I don't know what they mean. RRS feed

  • Question

  • Here my code:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class player : MonoBehaviour
    {
    float angle = 0;
    int xSpeed = 3;
    void Start()
    {
    }
    void Update()
    Moveplayer();
    void Moveplayer()
    {
    Vector2 pos = transform.position;
    pos.x = Mathf.Cos(angle)*3;
    pos.y = 0;
    transform.position * pos;
    angle += Time.deltaTime * xSpeed;
    }
    }

    I don't even know what I'm missing, But I'm also a horrible coder. I just want mt player to move properly.

    Tuesday, July 2, 2019 11:57 PM

All replies

  • Can you give us any clues as to what the bugs are? Or do we have to guess?
    Wednesday, July 3, 2019 12:00 AM
  • That overrides the position every time.  Usually you'd want to add something to the current position.  What you have there will have the position wiggle back and forth along the X axis, since you force Y to 0.

    I suspect you really wanted:

        pos.x += Mathf.Cos(angle) * 3;
        pos.y += Mathf.Sin(angle) * 3;

    but you still have to worry about exceeding the limits of your space.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Wednesday, July 3, 2019 12:06 AM
  • Hello,

     Try placing a semicolon at the end of method void update()

    or correct the scoping with proper brackets {}.  Ante is correct also.

    Hope this helps :)


    • Edited by User3DX Wednesday, July 3, 2019 12:53 AM
    Wednesday, July 3, 2019 12:51 AM
  • Yes absolutely. Here you go.

    Error #1:

    Assets\playerMovementUpdate.cs(line 18, line 5): error CS1519: Invalid token '{' in class, struct, or interface member declaration

    Error #2:

    Assets\playerMovementUpdate.cs(line 36, line 1): error CS1022: Type or namespace definition, or end-of-file expected

    Wednesday, July 3, 2019 3:42 AM
  • Greetings CC (I forgot the greeting last time. Sorry for my bad manners.)

    It looks like you have a couple of lines in the middle that probably would cause errors like that. Specifically you have a method called Update with no body (nothing between braces) and a superfluous definition of MovePlayer.

    And by the way, it's better to insert code via the tool on the toolbar than to show a screenshot. That way, we can easily copy and paste to make suggested improvements/corrections without having to retype anything. It's not a big deal in this case, but for long code where we need to make a lot of edits, it matters.

    Wednesday, July 3, 2019 5:20 AM
  • Hi CodingCowboy,

    Thank you for posting here.

    Since this thread is related to Unity, I suggest that you could post in the following forum.

    https://forum.unity.com/forums/scripting.12/

    The Visual C# forum discusses and asks questions about the C# programming language, IDE, libraries, samples, and tools.

    Note:This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 3, 2019 9:31 AM