Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 $$anonymous$$e 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.

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

How to access Blend Tree "speed" in Mecanim from C# 0 Answers

Left Click Script Problems 1 Answer

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges