Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Problem with 2d character controller

Discussion in 'Scripting' started by Crazyman582, Sep 24, 2015.

  1. Crazyman582

    Crazyman582

    Joined:
    Aug 13, 2015
    Posts:
    13
    Hi. I'm making a basic character controller to move the character along the y axis with the Vertical input. My script is
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerMove : MonoBehaviour {
    5.  
    6.     public float maxSpeed = 2;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.  
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update () {
    15.     //Returns a float from -1 to +1
    16.         Vector3 pos = transform.position ;
    17.         pos.y += Input.GetAxis ("Vertical") * maxSpeed * Time.deltaTime;
    18.         transform.position = pos * maxSpeed;
    19.      
    20.      
    21.      
    22.      
    23.     }
    24. }
    when variable maxSpeed = 1 the controller works perfectly. But when the maxSpeed is anything over one it doesn't work and I get this error message:

    transform.position assign attempt for 'Orange_Box64x64' is not valid. Input position is { -Infinity, -38391908889837727000000000000000000000.000000, 0.000000 }.
    UnityEngine.Transform:set_position(Vector3)
    PlayerMove:Update() (at Assets/PlayerMove.cs:18)

    As a newby in unity this has me utterly befuddled. Hoping a more experienced scriptor can help!

    one more question: How can i move the character along the x axis as well without using transform.position (it is already declared so can not be used again)
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Any reason why you are multiplying pos by maxspeed? Try this instead

    Code (CSharp):
    1. void Update () {
    2.         Vector3 pos = transform.position ;
    3.         pos.y += Input.GetAxis ("Vertical") * maxSpeed * Time.deltaTime;
    4.         transform.position = pos;
    5. }
    Adding in horizontal movement is trivial

    Code (CSharp):
    1. void Update () {
    2.         Vector3 pos = transform.position ;
    3.         pos.x += Input.GetAxis ("Horizontall") * maxSpeed * Time.deltaTime;
    4.         pos.y += Input.GetAxis ("Vertical") * maxSpeed * Time.deltaTime;
    5.         transform.position = pos;
    6. }
     
  3. Crazyman582

    Crazyman582

    Joined:
    Aug 13, 2015
    Posts:
    13
    Ok thanks. Dont have a chance to get on my computer right now but code looks like it will work!
    Thanks