Question by
PatrickKwon · Aug 03, 2020 at 10:17 AM ·
camera-movement
i need help with camera rotation and character movement
so i was trying to make a mobile game now i want to make the controller follow the camera rotation but it doesn;t work please help
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityStandardAssets.CrossPlatformInput;
public class movement : MonoBehaviour { // Start is called before the first frame update public Transform cam; private Rigidbody rb; //public Vector3 Movement; public FixedJoystick joystick; public float power = 10f; private Vector3 camForward; private Vector3 move; public float v, h;
private void Start()
{
cam = cam.transform; //make cam a trasnform
}
private void Awake()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
v = joystick.Horizontal;
h = joystick.Vertical;
rb.velocity = new Vector3(joystick.Horizontal * power, rb.velocity.y, joystick.Vertical * power );
// calculate move direction to pass to character
// calculate camera relative direction to move:
if(cam != null)
{
camForward = Vector3.Scale(cam.forward, new Vector3(1, 0, 1)).normalized;
move = v * cam.forward + h * cam.right;
}
else
{
// we use world-relative directions in the case of no main camera
move = joystick.Vertical * Vector3.forward + joystick.Horizontal * Vector3.right;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Cant figure out simple camera orbit 0 Answers
Movement of camera over time 1 Answer
Lerp Not working 1 Answer
How to stop the camera when the player has reached the edge of the level? 2 Answers