- Home /
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;
}
}
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.
first of all thank you so much for answering, i edit my question and put a link for a video that show the problem .
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.
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