Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 driku12_unity · Jul 02, 2020 at 12:13 AM · movementmovement scriptcamera rotatecamera movementcamera rotation

Camera-based movement is acting wonky

So, in short, I have a Cinemachine camera that follows my player around and which I can control the rotation of with my controller. I recently tried to add onto my script so that when the character moves with the joystick, it is relative to the direction the camera is pointing, instead of just the world axis. I watched several videos with similar methods, and I can't find anything I've done wrong with my code (Though judging by the results I've gotten, I definitely have somewhere), but for some reason when the player character moves, instead of the character's "forward" changing with the camera's "forward", the character's movement axis just flips from forward to backwards when the camera's y rotation goes from a positive number to a negative number.

Here is my code for my input and character movement:

using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using System.ComponentModel.Design; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Security.Cryptography; using System.Threading; using UnityEngine; using UnityEngine.InputSystem;

public class Ednacont : MonoBehaviour { PlayerControls controls;

 public Camera m_MainCamera;

 public CharacterController controller;

 public float speed = 6f;

 public float rotationspeed = 180f;

 Vector2 move;

 Quaternion targetRotation;

 // Start is called before the first frame update
 void Awake()
 {
     controls = new PlayerControls();
     controls.Gameplay.Move.performed += ctx => move = ctx.ReadValue<Vector2>();
     controls.Gameplay.Move.canceled += ctx => move = Vector2.zero;
 }

 void Update()
 {
     Vector3 camfwd = m_MainCamera.transform.forward;
     Vector3 camright = m_MainCamera.transform.right;

     camfwd.y = 0;
     camright.y = 0;
     camfwd.Normalize();
     camright.Normalize();


     Vector3 direction = new Vector3(-camfwd.x * -move.y, 0, camright.z * move.x).normalized;

     var input = new Vector3(camright.z * move.x, 0, -camfwd.x * move.y);

     if (input != Vector3.zero)
     {
         targetRotation = Quaternion.LookRotation(input);
     }

     transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, rotationspeed * Time.deltaTime);

     if (direction.magnitude >= 0.1f)
     {
         controller.Move(direction * speed * Time.deltaTime);
     }

 }

 void OnEnable()
 {
     controls.Gameplay.Enable();
 }

 void OnDisable()
 {
     controls.Gameplay.Disable();
 }

}

As I said, everything works perfectly except for the fact that for some reason the player's rotation with the camera is "all or nothing" in a sense. The player moves well, and rotates to face the direction they are moving perfectly, but the axis of their movement is strangely limited. If the camera's y rotation is positive, they move along the world axis as normal, and if it is negative, it flips and they move on what seems to be a negative version of the world axis, but nothing in between. The few things I can think of about my project that are different than the videos and examples I've seen is that I am using a character controller to move my character instead of transform.translate, and my camera is controlled by Cinemachine. I am not sure how that would affect it, but I know that my camera's transform rotates properly so I don't think that should be a problem. I haven't found anybody else on here who has had this exact problem, so I figured I'd post this one. Please let me know if you require more information, this was a little hard for me to describe, thank you!

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

197 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

Related Questions

FPS cam for Roll -a-Ball like game 1 Answer

My vector3 lerp is not working properly 1 Answer

Camera Moves or Player Moves for SideScroller 1 Answer

How to orbit camera around player from top-down to sidescroller view? 1 Answer

Touch buttons for step movememt 3 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