Search Unity

Player movement - My first game ever

Discussion in 'Scripting' started by FredZ_AppDeveloper, Sep 20, 2017.

  1. FredZ_AppDeveloper

    FredZ_AppDeveloper

    Joined:
    Sep 19, 2017
    Posts:
    4
    Hey guys:)
    I'm trying to make my first mobile game, but I'm pretty much stuck.
    So, as you guys can see in the video, I posted of my game, the ball (Ball is the player) can only turn left and forward when I click on space button. How can I get the ball to turn the way that the road is turning? You know, the 3D object that the ball is driving on...

    Hope you guys understand what I mean, feel free to ask:)

    Here's the video showing the game, and script:
     
  2. FredZ_AppDeveloper

    FredZ_AppDeveloper

    Joined:
    Sep 19, 2017
    Posts:
    4
    Here's my script:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class PlayerController : MonoBehaviour {

    public float speed;

    private Vector3 dir;

    // Use this for initialization
    void Start ()
    {
    dir = Vector3.zero;
    }

    // Update is called once per frame
    void Update ()
    {
    if (Input.GetKeyDown(KeyCode.Space))
    {
    if (dir == Vector3.forward)
    {
    dir = Vector3.left;
    }
    else
    {
    dir = Vector3.forward;
    }
    }

    float amoutToMove = speed * Time.deltaTime;

    transform.Translate(dir * amoutToMove);
    }
    }
     
  3. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    One way I've seen this done in the past is to put a trigger at each turn, which will notify the player of which way to turn when space is pressed. That method can also be used for jumping and other path actions.
     
  4. FredZ_AppDeveloper

    FredZ_AppDeveloper

    Joined:
    Sep 19, 2017
    Posts:
    4
    Thanks, I'm trying it:)
     
  5. FredZ_AppDeveloper

    FredZ_AppDeveloper

    Joined:
    Sep 19, 2017
    Posts:
    4
    Okay, so
    Okay so, I've added a component called "Event Trigger", but there's a lot of options to choose, do you know which one, I should choose?
     

    Attached Files:

  6. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807