Search Unity

Controlling a Character

Discussion in 'Editor & General Support' started by TheRedGuy90, Feb 1, 2015.

  1. TheRedGuy90

    TheRedGuy90

    Joined:
    Dec 5, 2014
    Posts:
    117
    Is the "Character Controller" all I need to turn an object into my main player?
     
  2. Trimadix

    Trimadix

    Joined:
    Jan 13, 2014
    Posts:
    10
    Hi TheRedGuy90,

    I would reccomend to delete additional cameras if there any exists, unless you plan to create a system or a mechanic which would allow moving between the views.
    As of your question - Yes, "Character Controller" will make the object it handles the main player.

    If you need additional information about it, you can read it up here
    http://docs.unity3d.com/Manual/class-CharacterController.html

    Sincerely,
    Trimadix
     
  3. TheRedGuy90

    TheRedGuy90

    Joined:
    Dec 5, 2014
    Posts:
    117
    my player doesnt move.
    are there awkward keys assigned to movement? I'm just using the arrow keys.
    Do I need to add a "jump" component if I want my character to jump?
     
  4. TheRedGuy90

    TheRedGuy90

    Joined:
    Dec 5, 2014
    Posts:
    117
    The details of my current project are a cube on a terrain, and I just assigned a Character Controller to the cube.
     
  5. Trimadix

    Trimadix

    Joined:
    Jan 13, 2014
    Posts:
    10
    I didnt think you were talking about the component. Well as of the component, no, it certainly doesnt make your game object your main player and it doesnt involve movement mechanics themselves, it only includes and consists of different mechanics of physics like slope detection, distance between slopes and the object etc.

    If you want to play around with movement and youre new I would advice you to go to the top of Unity where you find all the tabs for adding objects and manipulating the game (file, edit, assets etc). Click Assets -> Import Package -> Character Controllers. Youll see that it will add a new folder and youll find a nicely configurated fps and 3d person models. When youll add them to your map, theyll automatically become your main player with cameras and scripts attached to them.

    If you want to move around with using assets and just blunt code, you could go ahead and create a new script.
    Ill give you a C# copy of moving around, though it still requires modifications such as jump, walk etc. There could be a lot improved, but I assume youre looking for the basics and its easy to read.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class MovementScript : MonoBehaviour {
    4.     public float moveSpeed = 5f;
    5.     // Use this for initialization
    6.     void Start () {
    7.  
    8.     }
    9.  
    10.     // Update is called once per frame
    11.     void FixedUpdate () {
    12.  
    13.         if (Input.GetKey(KeyCode.D))
    14.         {
    15.             gameObject.transform.Translate(moveSpeed * 0.05f,0,0);
    16.         }
    17.         if (Input.GetKey(KeyCode.A))
    18.         {
    19.             gameObject.transform.Translate(-moveSpeed * 0.05f,0,0);
    20.         }
    21.         if (Input.GetKey(KeyCode.W))
    22.         {
    23.             gameObject.transform.Translate(0,0,moveSpeed * 0.05f);
    24.         }
    25.         if (Input.GetKey(KeyCode.S))
    26.         {
    27.             gameObject.transform.Translate(0,0,-moveSpeed * 0.05f);
    28.         }
    29.     }
    30. }
     
    TheRedGuy90 likes this.
  6. TheRedGuy90

    TheRedGuy90

    Joined:
    Dec 5, 2014
    Posts:
    117
    I installed the 3rd person controller from the assets.
    My cube gets suspended in the air as soon as I hit an arrow key for some reason. It can jump, but just stays about 10 off the ground and cant go anywhere.
     
    Last edited: Feb 3, 2015
  7. Jaimi

    Jaimi

    Joined:
    Jan 10, 2009
    Posts:
    6,208
  8. TheRedGuy90

    TheRedGuy90

    Joined:
    Dec 5, 2014
    Posts:
    117
    My character is not humanoid. It is a 2D Sprite in 3D.
     
  9. TheRedGuy90

    TheRedGuy90

    Joined:
    Dec 5, 2014
    Posts:
    117
    What do I add to this to detect collision; adding a Rigidbody has too many adverse effects.
     
  10. TheRedGuy90

    TheRedGuy90

    Joined:
    Dec 5, 2014
    Posts:
    117
    In the new version, I get an error "movespeed does not exist in the current context. I changed the parentheses to brackets, and get the error "only assignment, call etc. can be used as a statement". What's changed
     
  11. Jaimi

    Jaimi

    Joined:
    Jan 10, 2009
    Posts:
    6,208
    What's happening is you're trying to write a computer program, without knowing how to program. I recommend to learn C#. Then you'll know things like: You can't just change parenthesis to brackets, these things mean different things. And when it says "movespeed" does not exist, it's different from "moveSpeed" (notice the capital S).

    Here's some links to get you started.

    https://channel9.msdn.com/Series/C-Sharp-Fundamentals-Development-for-Absolute-Beginners

    https://www.udemy.com/programming-for-complete-beginners-in-csharp/
     
  12. TheRedGuy90

    TheRedGuy90

    Joined:
    Dec 5, 2014
    Posts:
    117
    Well, moveSpeed isn't recognized either.

    And this script worked just fine in the earlier version of Unity before my computer died on me and I had to reinstall.
     
  13. Jaimi

    Jaimi

    Joined:
    Jan 10, 2009
    Posts:
    6,208
    It would never work changing parenthesis to brackets.

    Also, "moveSpeed" is clearly at the top of the script:

    public float moveSpeed = 5f;

    So check all your usages of it, make sure you don't have some extra braces in places where they shouldn't be.

    And take one of those courses, so you know how C# works.