Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 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
1
Question by Ahmed-Hasoon · Sep 05, 2020 at 07:20 PM · camera3dmobileplayer movementjoysticks

my character movement act weirdly when i activate my camera follow script

Hello Everyone

I'm trying to use twin joystick controllers for my 3D game, one for movement and the other to rotate the player on the x axis (aim). i achieved that and I'm happy with the result and the movement of my character but when i try to use a script for a camera to follow my player, my controls will mess up only on vertical (forward) movement. left, right and down movement are all ok (i don't know how to really explain the situation it has been a week since I'm trying to solve this , but what i can say is that when i try to move the character forward (push the handle up) and my camera script is activated it get messed up and all the movement starts to act different ) i tried to use cinemachine its gives the same result, also tried to use an empty object that follow the player and i let the camera follow the empty object and it didn't work (gives the same result),so basically when i use only the controllers script its works perfectly the same for camera follow script its works perfectly for another project.and i use the free joysticks pack from the unity assets store.


and here is a link of a video about the problem [https://drive.google.com/file/d/1Crx7HjPp2yQ8vgunMLxwcM9k2ElgHXNX/view?usp=sharing]


The code i use for the controllers:

 using System.Collections;
 using System.Collections.Generic;
 using System.Threading;
 using UnityEngine;
 
 public class PlayerJoystickController : MonoBehaviour
 {
     public FixedJoystick moveJoystick;
     public FixedJoystick lookJoystick;
     
     void Update()
     {
         UpdateMoveJoystick();
         UpdateLookJoystick();
     }
 
     void UpdateMoveJoystick()
     {
         float hoz = moveJoystick.Horizontal;
         float ver = moveJoystick.Vertical;
         Vector2 convertedXY = ConvertWithCamera(Camera.main.transform.position, hoz, ver);
         Vector3 direction = new Vector3(convertedXY.x, 0, convertedXY.y).normalized;
         transform.Translate(direction * 10f*Time.deltaTime, Space.World);
     }
 
     void UpdateLookJoystick()
     {
         float hoz = lookJoystick.Horizontal;
         float ver = lookJoystick.Vertical;
         Vector2 convertedXY = ConvertWithCamera(Camera.main.transform.position, hoz, ver);
         Vector3 direction = new Vector3(convertedXY.x, 0, convertedXY.y).normalized;
         Vector3 lookAtPosition = transform.position + direction;
         transform.LookAt(lookAtPosition);
     }
 
 
     private Vector2 ConvertWithCamera(Vector3 cameraPos, float hor, float ver)
     {
         Vector2 joyDirection = new Vector2(hor, ver).normalized;
         Vector2 camera2DPos = new Vector2(cameraPos.x, cameraPos.z);
         Vector2 playerPos = new Vector2(transform.position.x, transform.position.z);
         Vector2 cameraToPlayerDirection = (Vector2.zero - camera2DPos).normalized;
         float angle = Vector2.SignedAngle(cameraToPlayerDirection, new Vector2(0, 1));
         Vector2 finalDirection = RotateVector(joyDirection, -angle);
         return finalDirection;
     }
 
     public Vector2 RotateVector(Vector2 v, float angle)
     {
         float radian = angle * Mathf.Deg2Rad;
         float _x = v.x * Mathf.Cos(radian) - v.y * Mathf.Sin(radian);
         float _y = v.x * Mathf.Sin(radian) + v.y * Mathf.Cos(radian);
         return new Vector2(_x, _y);
     }
 }



The code i use for camera follow:

 using UnityEngine;
 
 public class CameraFollow : MonoBehaviour
 {
     public Transform target;
     public float smoothSpeed ;
     public Vector3 offset;
 
     void LateUpdate()
     {
 
         Vector3 desiredPosition = target.position+ offset;
         Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed * Time.deltaTime);
         transform.position = smoothedPosition;
 
     }
 }




Comment
Add comment · Show 2
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 BenWiller1989 · Sep 06, 2020 at 12:39 AM 0
Share

Describe the weirdness that's happening. You are using the Camera position for your player $$anonymous$$ovement and set the camera pos to the target pos (probably the player with a offset negative z). Try to debug it out. If you can describe the weirdness, we might be able to do something.

avatar image Ahmed-Hasoon BenWiller1989 · Sep 07, 2020 at 05:23 PM 0
Share

first of all thank you so much for answering, i edit my question and put a link for a video that show the problem .

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Fkat · Sep 06, 2020 at 12:03 AM

@ Ahmed-Hasoon I think the problem might be that you are having your character move in relation to world space as opposed the its self space (Space.Self). Maybe this isn't the issue but I wanted to throw that out there anyway.

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 Ahmed-Hasoon · Sep 07, 2020 at 05:26 PM 0
Share

thanks a lot for your answer, i edit my question and put a link for a video that show the problem, i hope you watch it and help me fix the issue

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

292 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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 turn a corner and have the camera and player keep going in direction the corner is leading to. 0 Answers

Move player based on camera direction (MOBILE JOYSTICK) 0 Answers

Rotation of player based on camera direction and joystick direction in 3D world 1 Answer

How to force the game to be stretched to fit the screen? 1 Answer

Off center projection matrix does not move skybox 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