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. Dismiss Notice

Zero Errors, Yet Zero Movement - C# Script ( Character Controller )

Discussion in 'Scripting' started by Cody-Rauh, Jan 24, 2016.

  1. Cody-Rauh

    Cody-Rauh

    Joined:
    Oct 12, 2013
    Posts:
    256
    I am trying to create a basic controller to allow a player to be a fish, once I have something basic then build on to that slight sinks, swim, turn, idle animations,etc... everything one step at a time.

    Problem zero errors in console yet no movement :(

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class FishController : MonoBehaviour
    5. {
    6.     public float speed = 6.0f;
    7.  
    8.     private Vector3 moveDirection = Vector3.zero;
    9.  
    10.     void Update()
    11.     {
    12.         CharacterController controller = GetComponent<CharacterController>();  
    13.         {
    14.             moveDirection = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
    15.             moveDirection = transform.TransformDirection (moveDirection);
    16.             moveDirection *= speed;
    17.         }
    18.         controller.Move (moveDirection * Time.deltaTime);
    19.     }
    20. }
    21.  
     
  2. Cody-Rauh

    Cody-Rauh

    Joined:
    Oct 12, 2013
    Posts:
    256
    Also anyone know what the error is at the bottom of this screenshot?

     
  3. mickmarona

    mickmarona

    Joined:
    Apr 7, 2015
    Posts:
    44
    That's more of a warning than an error; it happens because the Unity-supplied script template has MacOS line endings, whereas if you're using Windows for example, any lines you add to the script will have Windows line endings instead; this mismatch in endings can cause trouble for the compiler. You can fix this by modifying the template itself to have the line endings you want, or by converting the line endings each time you make a new script. I've never run into a problem with having different line endings myself, however.

    As for your core question, only thing I can think of is that TransformDirection() is doing something weird. Here's how I handle movement vectors for the character controller:

    Code (CSharp):
    1. Vector3 hor = Input.GetAxis("Horizontal") * transform.right;
    2. Vector3 ver = Input.GetAxis("Vertical") * transform.forward;
    3. Vector3 movement = hor + ver;
    You can replace the forward/right with something else, such as the camera's forward/right. As it is, it will use the orientation of whatever the fish controller is attached to.
     
    Last edited: Jan 24, 2016
  4. Cody-Rauh

    Cody-Rauh

    Joined:
    Oct 12, 2013
    Posts:
    256
    Assets/Fish Swim Assets/Scripts/FishController.cs(14,150): error CS1503: Argument `#1' cannot convert `UnityEngine.Vector3' expression to type `float'
     
  5. mickmarona

    mickmarona

    Joined:
    Apr 7, 2015
    Posts:
    44
    Oh, whoops; sorry about that, it should actually be:

    Code (CSharp):
    1. Vector3 hor = Input.GetAxis("Horizontal") * transform.right;
    2. Vector3 ver = Input.GetAxis("Vertical") * transform.forward;
    3. Vector3 movement = hor + ver;
     
  6. Cody-Rauh

    Cody-Rauh

    Joined:
    Oct 12, 2013
    Posts:
    256
    found the issue... forgot to add speed... my previous code I posted works fine I accidently left speed in public var 0
     
  7. Cody-Rauh

    Cody-Rauh

    Joined:
    Oct 12, 2013
    Posts:
    256

    No worries and I appreciate any help at all... btw if I wanted to move up and down... what is the other axis besides horizontal, vertical axis?
     
  8. mickmarona

    mickmarona

    Joined:
    Apr 7, 2015
    Posts:
    44
    You can add any axis you want in the Input manager, under Edit > Project Settings > Input. Not sure what would be ideal in this case; perhaps spacebar for the positive, left ctrl for the negative.
     
  9. Cody-Rauh

    Cody-Rauh

    Joined:
    Oct 12, 2013
    Posts:
    256
    Well I would like to have up and down or w and s control up and down as people we normally think of it.
    then a and d or left and right be turning...
    then holding left click on mouse or space to go.

    However thinking about it would be really nice just to have mouse control which direction your fish goes when you hold left mouse then you move mouse around to make him go up and down.

    cause would make it really easy...

    left button foward
    right button back

    then fish moving your mouse changes fish direction, but don't know of any examples with that type of movement.
     
  10. Cody-Rauh

    Cody-Rauh

    Joined:
    Oct 12, 2013
    Posts:
    256
    So I just realized unity has a camera control mouselook script. Got that added which was a little bit of a pain cause conversion from 4 to 5, but success now just gotta look at how to make button press of the mouse to make fish move forward. :)
     
  11. Cody-Rauh

    Cody-Rauh

    Joined:
    Oct 12, 2013
    Posts:
    256
    All issues resolved, thanks for the help Mickmarona
     
    mickmarona likes this.