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 /
  • Help Room /
avatar image
0
Question by a03234833 · Apr 05, 2017 at 05:16 PM · rotationdirectiontransform.rotationvector 3

I can't coexist by character direction of camera vector AND character rotate by Input

I have the full code of my character controller, my code can independent operation of character direction of the camera vector when the camera rotate by mouse AND Character rotate facing the input direction

but CAN'T USE THESE TWO CODE AT THE SAME COMPILED PROGRAM

the problem code is at the end.

How can I solve this problem with just modify or add some code?

Thanks.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CharacrterControllerTest : MonoBehaviour
 {
 
     public CharacterController MyController;
     GameObject character;
 
     // Update is called once per frame
     public float Speed = 0f;
     public float WalkSpeed = 3f;
     public float RunSpeed = 6f;
     public float GravityStrength = 5f;
     public float JumpSpeed = 10f;
     public Transform CameraTransform;
 
     public Camera targetCamera;
 
     private Camera cam;
     private float currentX = 0.0f;
 
     //private Transform m_Cam;                  // A reference to the main camera in the scenes transform
     private Vector3 m_CamForward;             // The current forward direction of the camera
 
     float _doubleTapTime;
 
     private void AlignToCamera()
     {
         transform.rotation = Quaternion.Euler(transform.eulerAngles.x, targetCamera.transform.eulerAngles.y, transform.eulerAngles.z);
     }
 
 
 
     // Use this for initialization
     void Start()
     {
 
     }
 
     // Update is called once per frame
 
     void Update()
     {
 
         //get input from the player
         float moveHorizontal = Input.GetAxis("Horizontal");
         float moveVertical = Input.GetAxis("Vertical");
 
 
         //X, Y, Z
         Vector3 myVector = new Vector3(moveHorizontal, 0.0f, moveVertical);
 
         Vector3 NextDir = new Vector3(moveHorizontal, 0.0f, moveVertical);
 
         // Get the direction of the camera
         Vector3 moveDirection = Camera.main.transform.forward;
         // Make this vector planar (xz-plane)
         moveDirection.y = 0.0f;
         // Normalize to get a unit vector (optional)
         Vector3.Normalize(moveDirection);
 
         myVector = myVector * Speed * Time.deltaTime;
 
         bool doubleTap = false;
 
         #region doubleTap
 
         if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.D))
         {
             if (Time.time < _doubleTapTime + .3f)
             {
                 doubleTap = true;
             }
             _doubleTapTime = Time.time;
         }
 
         #endregion
 
         if (Input.GetKey(KeyCode.W) == false && Input.GetKey(KeyCode.A) == false && Input.GetKey(KeyCode.S) == false && Input.GetKey(KeyCode.D) == false)
         {
             Speed = WalkSpeed;
         }
 
         if (doubleTap)
         {
             Speed = RunSpeed;
         }
 
         myVector = CameraTransform.rotation * myVector;     //rotate input by direction of camera
 
         //how do we get the jump button?
         float verticalVelocity = MyController.velocity.y - GravityStrength * Time.deltaTime;
 
         if (Input.GetButtonDown("Jump") && MyController.isGrounded)
         {
             //add jumpspeed to vertical velocity
             verticalVelocity += JumpSpeed;
         }
 
         myVector.y = verticalVelocity * Time.deltaTime;
 
         // use input to move the character
         MyController.Move(myVector);
 
 
 
         //transform.Translate(myVector * Speed * Time.deltaTime, Space.World);
 
         if (myVector.x != 0 || myVector.z != 0)
         {
             // ↓ HERE IS THE MAIN PROBLEM!!
             transform.rotation = Quaternion.LookRotation(NextDir, Vector3.up);
             transform.rotation = Quaternion.LookRotation(moveDirection, Vector3.up);
             // ↑ HERE IS THE MAIN PROBLEM!!
         }
 
 
     }
 }
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

0 Replies

· Add your reply
  • Sort: 

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

112 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

Related Questions

How to make an object rotate in the direction using horzontal and vertical input axis? 0 Answers

Object rotates normaly while moving, then does zig zags? 0 Answers

Object rotate and slowly stop to toward 1 Answer

How to get the direction based on rotation 0 Answers

Locking facing direction or changing it after following the mouse position 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