- Home /
How do I get camera movement through controller input.
I tried adding the movement to the existing Vector3 but I didn't want it to keep going. I also didn't really want to use a bounding box to stop the camera, I think that would be clunky/glitchy. I am trying to get this effect from the camera. Any help is appreciated. I also would like to be able to add the rotation effect, but the main problem I am having is the position. The problem with the position is that with this it literally doesn't do anything. Any solutions?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraSwitcher : MonoBehaviour
{
[SerializeField] GameObject CockpitCamera;
[SerializeField] GameObject ChaseCamera;
bool CockpitCameraActive = true;
bool ChaseCameraActive = false;
Vector3 ChaseCameraPos;
void Update()
{
if(Input.GetKeyDown(KeyCode.C))
{
if(CockpitCamera.active)
{
CockpitCamera.SetActive(false);
ChaseCamera.SetActive(true);
}
else if (ChaseCamera.active)
{
CockpitCamera.SetActive(true);
ChaseCamera.SetActive(false);
}
}
if (ChaseCamera.active)
{
float h = Input.GetAxis("Horizontal");
ChaseCameraPos = ChaseCamera.transform.position;
ChaseCameraPos = new Vector3(h, 1.41f, -4.367f);
}
}
}
Well, you aren't using ChaseCameraPos
anywhere, yeah? This might be a simplified version of your actual script, but that's not being assigned to a transform as a position and it's not accessibly externally since it's a private value.
For that effect, I think you have the right idea that the camera's position can be taken from the actual stick direction (turning Right, the camera should strafe to the Right), but you'll want the camera's focus to be some point in front of the vehicle. Attaching a crude paint drawing.
Lol! First of all, I can't believe I made such a simple mistake. Second, thank you for your help, though I've tried a lot of things and cannot seem to implement what you are saying here. I might have to try something else.
Answer by SuperScience · Sep 20, 2019 at 06:46 PM
Hi Connor,
You are reading the current chase camera position, but you aren't assigning back the modified position.
For the time being, I recommend removing all of the activestate stuff so that you can get the "happy path" working.
But essentially here's the main issue: you are reading out the value... ChaseCameraPos = ChaseCamera.transform.position; .. and then you modify it... ChaseCameraPos = new Vector3(h, 1.41f, -4.367f); .. but you never write back. (Actually in the 2nd line, you're overwriting the previous line).
I think that what you want to do is ChaseCamera.transform.position = new Vector3(h, 1.41f, -4.367f);
Thank you, I really don't know how I didn't think about it overwriting.
I'm not sure what your game does, but I think I would recommend using transform.Translate for most cases.
transform.Translate(Vector3.forward h Time.deltaTime);
or Vector3.up, left (whatever direction you want positively assigned to the horizontal axis).
I was going to use transform.Translate, but I'm pretty sure that isn't limited to just -1 to 1. The only reason I wrote that code like that was that I wanted bounds where the camera would stop.
Your answer
Follow this Question
Related Questions
Move camera when mouse is near the edges of the screen 1 Answer
Need help updating camera position to go above the player 2 Answers
Multiple Targets Camera 1 Answer
How to spawn objects on a restricted sphere surface relative to camera? 1 Answer
How to make camera move like it does in the scene viewport? 0 Answers