Click here to Skip to main content
15,567,466 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
/// <summary>
/// Responsible for moving the player automatically and
/// reciving input.
/// </summary>
[RequireComponent(typeof(Rigidbody))]
 
 
public class PlayerBehaviour : MonoBehaviour
{
    /// <summary>
    /// A reference to the Rigidbody component
    /// </summary>
    private Rigidbody rb;
 
    [Tooltip("how fast the ball moves left/right")]
    public float dodgeSpeed = 5;
 
    [Tooltip("how fast the ball moves forward automatically")]
    [Range(0, 10)]
    public float rollSpeed = 5;
 
    public enum MobileHorizMovement
    {
        Accelerometer,
        ScreenTouch
    }
 
    public MobileHorizMovement horizMovement = MobileHorizMovement.Accelerometer;
 
    [Header("Swipe Properties")]
    [Tooltip("How far will the player move upon swiping")]
    public float swipeMove = 2f;
 
 
    [Tooltip("How far must the player swipe before we will execute the action (in pixel space)")]
    public float minSwipeDistance = 2f;
 
    /// <summary>
    /// Stores the starting position of mobile touch events
    /// </summary>
    private Vector2 touchStart;
 
    /// <summary>
    /// Use for initialization
    /// </summary>
    void Start()
    {
        //get access to our Rigidbody component
        rb = GetComponent<Rigidbody>();
    }
 
    /// <summary>
    /// Will figure out where to move the player horizontally
    /// </summary>
    /// <param name="pixelPos">The position the player has
    ///                         touched/clicked on</param>
    /// <returns>The direction to move in the x axis</returns>
    float CalculateMovement(Vector3 pixelPos)
    {
        // Converts to a 0 to 1 scale
        var worldPos = Camera.main.ScreenToViewportPoint(pixelPos);
 
        float xMove = 0;
 
        // If we press the right side of the screen
        if (worldPos.x < 0.5f)
        {
            xMove = -1;
        }
        else
        {
            // Otherwise we're on the left
            xMove = 1;
        }
 
        // replace horizontalSpeed with our own value
        return xMove * dodgeSpeed;
    }
 
 
    /// <summary>
    /// Update is called once per frame
    /// </summary>
    void Update()
    {
        // Movement in the x axis
        float horizontalSpeed = 0;
 
        //check if we are running either in the Unity editor or in a
        //standalone build.
#if UNITY_STANDALONE || UNITY_WEBPLAYER
       
        //check if we're moving to the side
        horizontalSpeed = Input.GetAxis("Horizontal") *
                                     dodgeSpeed;
 
        // If the mouse is held down (or the screen is tapped
        // on Mobile
        if (Input.GetMouseButton(0))
        {
             horizontalSpeed = CalculateMovement(Input.mousePosition);
        }
 
        //Check if we are running on a mobile device
#elif UNITY_IOS || UNITY_ANDROID
 
        if(horizMovement == MobileHorizMovement.Accelerometer)
        {
           // Move player based on direction of the accelerometer
           horizontalSpeed = Input.acceleration.x * dodgeSpeed;
        }
 
        // Check if Input has registered more than zero touches
        if (Input.touchCount > 0)
        {
           // Store the first touch detected.
           Touch touch = Input.touches[0];
 
           if(horizMovement == MobileHorizMovement.ScreenTouch)
           {
               horizontalSpeed = CalculateMovement(touch.position);
           }
       
#endif
 
        // apply our automove and movement force
        rb.AddForce(horizontalSpeed, 0, rollSpeed);
 
    }
    /// <summary>
    /// Will teleport the player if swiped to the left or right
    /// </summary>
    /// <param name="touch">Current touch event</param>
    void SwipeTeleport(Touch touch)
    {
        // Check if the touch just started
        if (touch.phase == TouchPhase.Began)
        {
            // If so, set touchStart
            touchStart = touch.position;
        }
 
        // If the touch has ended
        else if (touch.phase == TouchPhase.Ended)
        {
            // Get the position the touch ended at
            Vector2 touchEnd = touch.position;
 
            // Calculate the difference between the beginning and
            // end of the touch on the x axis.
            float x = touchEnd.x - touchStart.x;
 
 
            // If we are not moving far enough, don't do the teleport
 
            if (Mathf.Abs(x) < minSwipeDistance)
            {
                return;
            }
 
            Vector3 moveDirection;
 
            // If moved negatively in the x axis, move left
            if (x < 0)
            {
                moveDirection = Vector3.left;
            }
            else
            {
                // Otherwise we're on the right
                moveDirection = Vector3.right;
            }
 
            RaycastHit hit;
 
            // Only move if we wouldn't hit something
            if (!rb.SweepTest(moveDirection, out hit, swipeMove))
            {
                // Move the player
                rb.MovePosition(rb.position + (moveDirection *
                                swipeMove));
            }
        }
    }
    /// <summary>
    /// Will determine if we are touching a game object and if so
    /// call events for it
    /// </summary>
    /// <param name="touch">Our touch event</param>
    void TouchObjects(Touch touch)
    {
        // Convert the position into a ray
        Ray touchRay = Camera.main.ScreenPointToRay(touch.position);
 
        RaycastHit hit;
 
        // Are we touching an object with a collider?
        if (Physics.Raycast(touchRay, out hit))
        {
            // Call the PlayerTouch function if it exists on a
            // component attached to this object
            hit.transform.SendMessage("PlayerTouch",
                              SendMessageOptions.DontRequireReceiver);
        }
    }
}


What I have tried:

i've tried adding a second bracket on the bottom but it still keeps showing up
Posted
Updated 13-Sep-20 21:19pm
v2

1 solution

There is a closing } missing in this function:
C#
/// <summary>
/// Update is called once per frame
/// </summary>
void Update()
{
    // Movement in the x axis
    float horizontalSpeed = 0;

    //check if we are running either in the Unity editor or in a
    //standalone build.
    #if UNITY_STANDALONE || UNITY_WEBPLAYER

    //check if we're moving to the side
    horizontalSpeed = Input.GetAxis("Horizontal") *
    dodgeSpeed;

    // If the mouse is held down (or the screen is tapped
    // on Mobile
    if (Input.GetMouseButton(0))
    {
        horizontalSpeed = CalculateMovement(Input.mousePosition);
    }

    //Check if we are running on a mobile device
    #elif UNITY_IOS || UNITY_ANDROID

    if(horizMovement == MobileHorizMovement.Accelerometer)
    {
        // Move player based on direction of the accelerometer
        horizontalSpeed = Input.acceleration.x * dodgeSpeed;
    }

    // Check if Input has registered more than zero touches
    if (Input.touchCount > 0)
    {
        // Store the first touch detected.
        Touch touch = Input.touches[0];

        if(horizMovement == MobileHorizMovement.ScreenTouch)
        {
            horizontalSpeed = CalculateMovement(touch.position);
        }

        #endif

        // apply our automove and movement force
        rb.AddForce(horizontalSpeed, 0, rollSpeed);

    }

Advice: Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
Indentation style - Wikipedia[^]

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
 
Share this answer
 
Comments
CPallini 14-Sep-20 2:18am    
5.
Patrice T 14-Sep-20 2:19am    
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900