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 nrvllrgrs · Jan 31, 2013 at 03:27 AM · cameramovementmecanimavatar

How do I move a Mecanim driven character relative to the camera?

I'm trying to create a camera controller and character motor pair so that the game behaves similar to Kingdoms of Amalur. The example controller in Mecanim has WASD moving the avatar relative to itself (and then the camera follows the avatar). Instead I want WASD to move the avatar relative to the camera, which is controlled with the mouse. So W always moves the avatar away from the camera, and S always moves the avatar toward the camera. I have the camera controller working (code below). But I am having problems modifying the animation graph and implementing a motor to do so. Can somebody please help? Thanks in advance.

 using UnityEngine;
 using System.Collections;
 
 public class ThirdPersonCamera : MonoBehaviour
 {
     public GameObject _lookAtTarget;
     public Vector3 _lookAtOffset;
     
     public float _minPitchAngle = -20f;
     public float _maxPitchAngle = 30f;
     
     public float _sensitivity = 5f;
     public float _springStiffness = 0f;
     
     void LateUpdate ()
     {        
         Vector3 eulerRotation = transform.localEulerAngles;
         
         bool useController = (Input.GetJoystickNames().Length > 0);
         eulerRotation.y += (useController ? Input.GetAxis("Joystick X") : Input.GetAxis("Mouse X")) * _sensitivity;
         eulerRotation.x -= (useController ? Input.GetAxis("Joystick Y") : Input.GetAxis("Mouse Y")) * _sensitivity;
         
         if (eulerRotation.x > 180f)
         {
             eulerRotation.x -= 360f;
         }
         eulerRotation.x = Mathf.Clamp(eulerRotation.x, _minPitchAngle, _maxPitchAngle);
         
         Quaternion rotation = Quaternion.Euler(eulerRotation);
         transform.position = _lookAtTarget.transform.position - (rotation * _lookAtOffset);
         transform.LookAt(_lookAtTarget.transform);
     }
 }

Comment
Add comment · Show 1
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 FuzzyLuke · Feb 21, 2013 at 11:29 PM 0
Share

have you figured this out? i did it using ThirdPersonController scripting but now I can't port that to mecanim, I don't really know what I need to do

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by nrvllrgrs · Feb 24, 2013 at 04:26 PM

Here's my rough version right now. It's not quite right yet, but it's closer than it was before. If you have any revisions, please do share.

 using UnityEngine;
 using System.Collections;
 
 public class IdleRunJump : MonoBehaviour
 {
     private float _minSpeedThreshold = 1.55f;
     private float _maxSpeedThreshold = 5.5f;
     private float _maxAngularThreshold = 132.9878f;
     
     public float _speedDampTime = 0.25f;
     public float _angularSpeedDampTime = 0.25f;
     public bool ApplyGravity = true;
     
     protected Animator _animator;
     
     private int _speedId;
     private int _angularSpeedId;
     private int _directionId;
     private int _jumpId;
 
     // Use this for initialization
     void Start () 
     {
         _animator = GetComponent<Animator>();
         
         if(_animator.layerCount >= 2)
             _animator.SetLayerWeight(1, 1);
         
         _speedId = Animator.StringToHash("Speed");
         _angularSpeedId = Animator.StringToHash("AngularSpeed");
         _directionId = Animator.StringToHash("Direction");
         _jumpId = Animator.StringToHash("Jump");
     }
         
     // Update is called once per frame
     void Update () 
     {
         if (_animator)
         {
             AnimatorStateInfo stateInfo = _animator.GetCurrentAnimatorStateInfo(0);            
             
             bool useController = (Input.GetJoystickNames().Length > 0);
               float horizontal = Input.GetAxis("Horizontal");
             float vertical = Input.GetAxis("Vertical");
             
             Vector3 forwardAvatar = new Vector3(transform.forward.x, 0f, transform.forward.z).normalized;
             
             Vector3 forward = Camera.mainCamera.transform.TransformDirection(Vector3.forward);
             forward.y = 0f;
             forward = forward.normalized;
             Vector3 right = new Vector3(forward.z, 0f, -forward.x);
             
             Vector3 direction = ((horizontal * right) + (vertical * forward)).normalized;
             float deltaAngle = Vector3.Angle(direction, forwardAvatar);
             float dot = Vector3.Dot(forwardAvatar, Vector3.Cross(direction, Vector3.up));
     
             _animator.SetFloat(
                 _speedId, ((horizontal*horizontal) + (vertical*vertical)) * (!useController && Input.GetButton("Walk") ? _minSpeedThreshold : _maxSpeedThreshold), _speedDampTime, Time.deltaTime
             );
             
             float headingOffset = deltaAngle * Mathf.Sign(dot);
             
             _animator.SetFloat(_angularSpeedId, headingOffset, _angularSpeedDampTime, Time.deltaTime);
             _animator.SetFloat(_directionId, headingOffset);
         }
     }
 }
Comment
Add comment · 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

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

10 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

Related Questions

Camera follow Avatar 1 Answer

rotate Y axis to face mouse 1 Answer

Player walks to the right? 1 Answer

How to add Pinch to Zoom? 0 Answers

Set Max Rotation On Weapon Sway 0 Answers


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