• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by looneygeorge · Jun 27, 2013 at 02:45 AM · c#mecanim

Script only makes character run forward

Hi,

Trying to use the Locomotion starter pack script from the Asset store to try and make my own fully functioning controller. I have one problem though, the script seems to always be making my character move forwards i.e. going from 0 to 6 speed float (Mecanim) when I press S as well as W. I can't really figure out what part I am missing.

 public class JoystickToEvents : MonoBehaviour 
 {
     public static void Do(Transform root, Transform camera, ref float speed, ref float direction)
     {
         Vector3 rootDirection = root.forward;
         float horizontal = Input.GetAxis("Horizontal");
         float vertical = Input.GetAxis("Vertical");
                 
         Vector3 stickDirection = new Vector3(horizontal, 0, vertical);
         
 
         // Get camera rotation.    
 
         Vector3 CameraDirection = camera.forward;
         CameraDirection.y = 0.0f; // kill Y
         Quaternion referentialShift = Quaternion.FromToRotation(Vector3.forward, CameraDirection);
 
         // Convert joystick input in Worldspace coordinates
         Vector3 moveDirection = referentialShift * stickDirection;
                 
         Vector2 speedVec =  new Vector2(horizontal, vertical);
         speed = Mathf.Clamp(speedVec.magnitude, -1, 1);      
 
         if (speed > 0.01f) // dead zone
         {
             Vector3 axis = Vector3.Cross(rootDirection, moveDirection);
             direction = Vector3.Angle(rootDirection, moveDirection) / 180.0f * (axis.y < 0 ? -1 : 1);
         }
         else
         {
             direction = 0.0f;
         }
     }
     
 }

Is there a line of code im missing in order to get my WalkBack state to start working correctly? Thanks!

John

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by bodec · Jun 27, 2013 at 09:27 PM

speed in there should be between -1 and 1 0 meaning standing the animations will control your speed that is set in the animation setup. coding for mecanim is a bit different than a character controller instead of coding speed being how fast to move you will code 1 being moving forward at full speed and -1 moving backward at full speed.

here is a simple movement script to help understand it

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour {
     private Animator _droidAnimator;
     private float _horMove;
     private float _vertMove;
 
 
     public float animSpeed = 1.5f;
     
     
     
     // Use this for initialization
     void Start () {
         _droidAnimator = GetComponent<Animator>();
         
     }
     
 
     void LateUpdate () {
         _horMove = Input.GetAxis("Horizontal");
         _vertMove = Input.GetAxis("Vertical");
         _droidAnimator.SetFloat("Direction", _horMove);
         _droidAnimator.SetFloat("Speed", _vertMove);
         _droidAnimator.speed = animSpeed;
         
             
         
 //        if(_controller.isGrounded){
             if(Input.GetButtonDown("Jump")){
                 _droidAnimator.SetBool("Jump", true);
                 
                         
             }else{
             _droidAnimator.SetBool("Jump", false);
         }
         
 
     
     }
 }


and a screen shot of my tree

alt text


capture006.png (40.3 kB)
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image looneygeorge · Jun 27, 2013 at 11:32 PM 1
Share

Tried your script and quickly changed mine over to your more understandable one. this was a very nice starting point and fixed my problems with walking backwards and I got the jumping referenced in the script to work easily.

Thankyou $$anonymous$$r. Bodec! :)

avatar image bodec · Jun 28, 2013 at 12:58 AM 0
Share

glad it helped

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Character Not Rotating 0 Answers

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Is there anyway to only show a part of a GUI image during the game? (C#) 1 Answer

Script Efficiency 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges