Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2015
    Posts
    1

    Creating 2D Games in Unity 4.5 #8 - Character Controller #4 Our Character Controller

    Hello Guy.
    As Stated in the title i am watching the Game development series

    but i am stuck, i got this player to flip in that episode but whenever it flips

    he does flip in the same location that he is in,

    for example if i am at 23 on the X axis , when flip his position will go to -23 on the X Axis

    ,, but normally when filled he should have stayed at positive 23 please help

    using UnityEngine;
    using System.Collections;

    public class Player : MonoBehaviour
    {

    private bool _isFacingRight;
    private CharacterController2D _controller;
    private float _normalizedHorizontalSpeed;

    public float MaxSpeed = 8;
    public float SpeedAccelerationOnGround = 10f;
    public float SpeedAccelerationInAir = 5f;

    public void Start()
    {
    _controller = GetComponent<CharacterController2D>();
    _isFacingRight = transform.localScale.x > 0;
    }

    public void Update()
    {
    HandleInput ();

    var movementFactor = _controller.State.IsGrounded ? SpeedAccelerationOnGround : SpeedAccelerationInAir;
    _controller.SetHorizontalForce (Mathf.Lerp (_controller.Velocity.x, _normalizedHorizontalSpeed * MaxSpeed, Time.deltaTime * movementFactor));
    }

    private void HandleInput ()
    {
    if (Input.GetKey (KeyCode.D)) {
    _normalizedHorizontalSpeed = 1;
    if (!_isFacingRight)
    Flip ();
    } else if (Input.GetKey (KeyCode.A)) {
    _normalizedHorizontalSpeed = -1;
    if (_isFacingRight)
    Flip ();
    } else {

    _normalizedHorizontalSpeed = 0;
    }
    if (_controller.CanJump && Input.GetKeyDown (KeyCode.Space))
    {
    _controller.Jump();
    }

    }

    private void Flip ()
    {
    transform.localScale = new Vector3 (-transform.localScale.x , transform.localScale.y, transform.localScale.z);
    _isFacingRight = transform.localScale.x > 0;
    }

    }

  2. #2
    Join Date
    Feb 2014
    Posts
    277
    The script you have posted looks fine so I don't think the problem is in that video.

    Try going back to the video called "Prefab and Player" and check the 23:53 mark where there is a screen showing the transform properties of the player in the Inspector just before Nelson creates the prefab of the player. In particular check that the transform Scale is 1 for x and 1 for y for the player. That x scale is what's supposed to change from 1 to -1 when you get a "Flip".

    From there I'd also check where Nelson is constructing the prefab of the Player, and also where the Player script is attached to the prefab. ("Getting Our Level Ready" at 0:48.)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •