- Home /
orbit camera around player using arrow keys.
I am trying to orbit my camera around the player using the arrow keys. I have my game set up to click and you move where you click. I would also like to scroll in and out to zoom in and out a little, this is what code I have at the moment.
using UnityEngine;
using System.Collections;
public class cameraOrbit : MonoBehaviour
{
public float speed = 0.1f;
public void FixedUpdate()
{
if(Input.GetKey(KeyCode.RightArrow))
{
transform.position = new Vector3(transform.position.x + speed, transform.position.y, transform.position.z);
}
if(Input.GetKey(KeyCode.LeftArrow))
{
transform.position = new Vector3(transform.position.x - speed, transform.position.y, transform.position.z);
}
if(Input.GetKey(KeyCode.DownArrow))
{
transform.position = new Vector3(transform.position.x, transform.position.y - speed, transform.position.z);
}
if(Input.GetKey(KeyCode.UpArrow))
{
transform.position = new Vector3(transform.position.x, transform.position.y + speed, transform.position.z);
}
}
}
Answer by xxmariofer · Jan 16, 2019 at 12:01 AM
but... you are not orbiting in that script you are only applying linear movement right?
You can use this other approach change it for you fixedupdate method and test it
void Update() { float direction = 0; if(input.GetKey(KeyCode.RightArrow)) { direction =1; } else if(Input.GetKey(KeyCode.LeftArrow)) { direction = -1; } transform.RotateArround(position, Vector3.up, speed time.deltaTime direction); }
Your answer
![](https://koobas.hobune.stream/wayback/20220612185828im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Making a bubble level (not a game but work tool) 1 Answer
Standard assets water bug 0 Answers
An OS design issue: File types associated with their appropriate programs 1 Answer