Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 lTimesl · Oct 24, 2019 at 01:37 AM · unity 5rotationcamera-movementcharacter movement

Rotate character with camera ?!

i am trying to modify the character movement script to make it rotate with the camera rotation

the problem i don't success to make it after reading some topic i get really confused about this issue

the code is able to create movement with cam direction but not rotation with cam

i tried to reference the cam "i use cinemachine" and link it to character rotation but this end with wired behavior "shaking move's"

here is the code thank you unity community

 public class MovmenetWithCamera : MonoBehaviour
 {
     Vector3 _CharacterDirection, realativedir;
     Quaternion _CharacterRotation;
     Animator _CharacterAnim;
     Rigidbody _CharacterRigidbody;
     [SerializeField]
     float TurnSpeed = 8, speed = 6;
     bool _IsWalking;
 
 
     void Start()
     {
         _CharacterAnim = GetComponent<Animator>();
         _CharacterRigidbody = GetComponent<Rigidbody>();
     }
 
     void Update()
     {
         CharacterActions();
     }
 
     void FixedUpdate()
     {
         CharacterMovement();
     }
 
     void CharacterMovement()
     {
         //movement input's
         float h = Input.GetAxis("Horizontal");
         float v = Input.GetAxis("Vertical");
         _CharacterDirection.Set(h, 0, v);

         // convert the character dir to cam dir
         realativedir = Camera.main.transform.TransformDirection(_CharacterDirection);
         realativedir.y = 0f;
         realativedir.Normalize();

         //bool movement check
         bool _IsHorizontalChange = !Mathf.Approximately(h, 0f);
         bool _IsVerticalChange = !Mathf.Approximately(v, 0);
         _IsWalking = _IsHorizontalChange || _IsVerticalChange;
         _CharacterAnim.SetBool("IsWalking", _IsWalking);
 
         //change character forward to the desired one
         Vector3 _DesairdForward = Vector3.RotateTowards(this.transform.forward, realativedir, TurnSpeed * Time.deltaTime, 0);
         _CharacterRotation = Quaternion.LookRotation(_DesairdForward);
 
         //RigidBody Direction && Rotation
         _CharacterRigidbody.MovePosition(_CharacterRigidbody.position + realativedir * speed * Time.deltaTime);
         _CharacterRigidbody.MoveRotation(_CharacterRotation);
 
     }
 
     void CharacterActions()
     {
         if (Input.GetKey(KeyCode.Space))
         {
             _CharacterAnim.SetTrigger("Shoot");
             Debug.Log("shoot");
         }
         else if (Input.GetKeyDown(KeyCode.Space) && _IsWalking)
         {
             _CharacterAnim.SetTrigger("Shoot");
 
         }
     }

 

 
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 lTimesl · Oct 24, 2019 at 07:17 AM

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MovmenetWithCamera : MonoBehaviour
 {
     Vector3 _CharacterDirection, realativedir;
     Quaternion _CharacterRotation, rot;
     Animator _CharacterAnim;
     Rigidbody _CharacterRigidbody;
     [SerializeField]
     float TurnSpeed = 8, speed = 6;
     bool _IsWalking;
     [SerializeField]
     Camera VCam;
 
 
     void Start()
     {
         _CharacterAnim = GetComponent<Animator>();
         _CharacterRigidbody = GetComponent<Rigidbody>(); 
     }
 
     void Update()
     {
         CharacterActions();
     }
 
     void FixedUpdate()
     {
         CharacterMovement();
     }
 
     void CharacterMovement()
     {
         //movement input's
         float h = Input.GetAxis("Horizontal");
         float v = Input.GetAxis("Vertical");
         _CharacterDirection.Set(h, 0, v);
 
         // convert the character dir to cam dir
         realativedir = VCam.transform.TransformDirection(_CharacterDirection);
         realativedir.y = 0f;
         realativedir.Normalize();
 
         //bool movement check
         bool _IsHorizontalChange = !Mathf.Approximately(h, 0f);
         bool _IsVerticalChange = !Mathf.Approximately(v, 0);
         _IsWalking = _IsHorizontalChange || _IsVerticalChange;
         _CharacterAnim.SetBool("IsWalking", _IsWalking);
 
         //change character forward to the desired one
         Vector3 _DesairdForward = Vector3.RotateTowards(this.transform.forward, realativedir, TurnSpeed * Time.deltaTime, 0);
         //_CharacterRotation = Quaternion.LookRotation(_DesairdForward); //use this for orbaiting camera with no rotation in move
 
         //rotate character with cam
         rot = VCam.transform.rotation;
         rot.x = 0;
         rot.z = 0;
         transform.rotation = rot;
         
 
         //RigidBody Direction && Rotation
         _CharacterRigidbody.MovePosition(_CharacterRigidbody.position + realativedir * speed * Time.deltaTime);
         //_CharacterRigidbody.MoveRotation(rot);
 
     }
 
     void CharacterActions()
     {
         if (Input.GetKey(KeyCode.Space))
         {
             _CharacterAnim.SetTrigger("Shoot");
             Debug.Log("shoot");
         }
         else if (Input.GetKeyDown(KeyCode.Space) && _IsWalking)
         {
             _CharacterAnim.SetTrigger("Shoot");
 
         }
     }
 }
 
Comment
Add comment · Show 1 · 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 lTimesl · Oct 24, 2019 at 07:19 AM 0
Share

the camera used is Cinemachine

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

243 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image

Related Questions

How can I rotate my camera when player has rigidbody? 0 Answers

Cross platform camera controlls 0 Answers

change rotation using WheelCollider.steerAngle 0 Answers

Stop camera from moving when parent rotates forward 1 Answer

CAR ROTATING HELP 2 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