- Home /
How to give camera relative movement to the player and I am using joystick for player and camera
My Player Script is =
using System.Collections; using System.Collections.Generic; using static System.Diagnostics.Debug; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; using static UnityEngine.Debug;
public class Ball : MonoBehaviour { // Player Movement Controls
protected Joystick Leftjoystick;
//protected Joybutton joybutton;
// Start is called before the first frame update
void Start()
{
Leftjoystick = FindObjectOfType<Joystick>();
//joybutton = FindObjectOfType<Joybutton>();
}
// Update is called once per frame
void Update()
{
var rigidbody = GetComponent<Rigidbody>();
rigidbody.velocity = new Vector3(Leftjoystick.Horizontal * 30f, rigidbody.velocity.y, Leftjoystick.Vertical * 30f);
}
}
My Camera Script is =
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class FreeCamera : MonoBehaviour { public VirtualJoystick cameraJoystick;
public Transform lookAt;
private float distance = 20.0f;
private float currentX = 0.0f;
private float currentY = 20.0f;
private float sensivityX = 3.0f;
private float sensivityY = 1.0f;
private void Update()
{
currentX += cameraJoystick.InputDirection.x * sensivityX;
currentY += cameraJoystick.InputDirection.z * sensivityY;
}
private void LateUpdate()
{
Vector3 dir = new Vector3(0, 0, -distance);
Quaternion rotation = Quaternion.Euler(currentY, currentX, 0);
transform.position = lookAt.position + rotation * dir;
transform.LookAt(lookAt);
}
}
My Joystick script is =
using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems;
public class VirtualJoystick : MonoBehaviour, IDragHandler, IPointerUpHandler, IPointerDownHandler {
private Image jsContainer;
private Image joystick;
public Vector3 InputDirection;
void Start()
{
jsContainer = GetComponent<Image>();
joystick = transform.GetChild(0).GetComponent<Image>(); //this command is used because there is only one child in hierarchy
InputDirection = Vector3.zero;
}
public void OnDrag(PointerEventData ped)
{
Vector2 position = Vector2.zero;
//To get InputDirection
RectTransformUtility.ScreenPointToLocalPointInRectangle
(jsContainer.rectTransform,
ped.position,
ped.pressEventCamera,
out position);
position.x = (position.x / jsContainer.rectTransform.sizeDelta.x);
position.y = (position.y / jsContainer.rectTransform.sizeDelta.y);
float x = (jsContainer.rectTransform.pivot.x == 1f) ? position.x * 2 + 1 : position.x * 2 - 1;
float y = (jsContainer.rectTransform.pivot.y == 1f) ? position.y * 2 + 1 : position.y * 2 - 1;
InputDirection = new Vector3(x, y, 0);
InputDirection = (InputDirection.magnitude > 1) ? InputDirection.normalized : InputDirection;
//to define the area in which joystick can move around
joystick.rectTransform.anchoredPosition = new Vector3(InputDirection.x * (jsContainer.rectTransform.sizeDelta.x / 3)
, InputDirection.y * (jsContainer.rectTransform.sizeDelta.y) / 3);
}
public void OnPointerDown(PointerEventData ped)
{
OnDrag(ped);
}
public void OnPointerUp(PointerEventData ped)
{
InputDirection = Vector3.zero;
joystick.rectTransform.anchoredPosition = Vector3.zero;
}
}
Can you edit your question so that there is a blank line before each code block? I think that should make the code more properly formatted and easier to read.
Answer by TheonlysiQ · Sep 19, 2020 at 09:25 PM
Make the camera a child that belongs to the player. Then, when you move the player, the camera will follow. Offset the initial transform coordinates to get the camera position you need.
Answer by Sameer34 · Sep 24, 2020 at 07:56 AM
I have Solved my problem by
By using new game object and added same script
Your answer
Follow this Question
Related Questions
Need help for player rotation and camera rotation 0 Answers
How do you move the camera relative to the direction it is currently facing? 1 Answer
GameObjects don't render every few frames 1 Answer
Calculate screen top and bottom postion 1 Answer
Can relative velocity be calculated from relative direction vector 3 Answers