Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by dragonking300 · Dec 15, 2016 at 04:07 PM · c#camerabeginner

How do I make a 3d person camera that changes with keyboard based input

I want to make a script that I could move my camera by doing things like pressing the x or c button. for example lets say the camera is looking north towards the player, I want the camera to look west and I would do that by pressing x or if I wanted it to look towards south so I would press c. How do I do that?

 using UnityEngine;
 using System.Collections;
 
 public class CameraControll : MonoBehaviour
 {
     public Transform lookAt;
     public Transform camtransform;
 
         private Camera cam;
 
     private float Distance = 10f;
     private float currentX = 0f;
     private float currentY = 0f;
     private float sesivityX = 4f;
     private float sesivityY = 1f;
 
     private void Start()
     {
         camtransform = transform;
         cam = Camera.main;
     }
 
     private void Update()
     {
         currentX += Input.GetAxis("Mouse X");
         currentY += Input.GetAxis("Mouse Y");
     }
 
 
 
     private void LateUpdate()
     {
         Vector3 dir = new Vector3(0, 0, -Distance);
         Quaternion rotation = Quaternion.Euler(currentY, currentX, 0);
         camtransform.position = lookAt.position + rotation * dir;
         camtransform.LookAt(lookAt.position);
     }
 
 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Creeper_Math · Dec 18, 2016 at 11:24 AM

A good setup for this would be a bunch of locations for the camera, and a script that (I would suggest) have a smooth pan...

This will not work initially.. What you will need to make sure is that the gameobejct[] arrays are set to a length of 3. Then you will need to make a set of empty gameobjects that will be the location variables. Suggested to be named "CameraLocation1" "CameraLookAt1" "CameraLocation2" "CameraLookAt2" etc (need to be 3 for the example below)... and put them into the arrays accordingly ("CameraLocation1" goes under CameraGoalPosition at entry "0" as well as "CameraLookAt1" under CameraGoalLookAt, etc...) For each one you will need to put then under the player gameobject, or another gameobject that has a script to set it's position to the player's position ( transform.position = player.transform.position ), which has all the objects as children of it (so the positions move relatively to the player's location)

Each object pair "CameraLocation1" "CameraLookAt1" represents a camera phase / position. For your example and what I sorta have put into the code, the following sets would work with the pairs

CameraLocation1 and CameraLookAt1 represent the position and lookat of the camera when the player wants to look north at the player (put location behind camera and lookat infront of camera CameraLocation2 and CameraLookAt2 represent looking west CameraLocation3 and CameraLookat3 represent looking east

  public GameObject[] CameraGoalLookAt;
  public GameObject[] CameraGoalPosition;
  public Vector3 CameraCurrentLookAt;
 
 public int selection; 
 
  public float MovementDividor;
  public float LookAtDividor;
 
 // The location setup 
 void Update() {
 if(Input.GetKeyDown(KeyCode.X)) {
 selection = 1;
 }
 if(Input.GetKeyDown(KeyCode.C)) {
 selection = 2;
 }
 if(Input.GetKeyDown(KeyCode.D)) { // Set it back to forwards
 selection = 0;
 }
 
 }
 
 // The camera detection / movement
  void LateUpdate() {
  // Camera Movement
  vector3 movement = CameraGoalPosition[selection] - transform.position;
  movement = movement / MovementDividor;
  transform.position += movement;
  
  // Camera Look At
  vector3 movementA = CameraGoalLookAt[selection] - CameraCurrentLookAt;
  movementA = movementA / LookAtDividor;
  CameraCurrentLookAt += movementA;
  transform.lookat(CameraCurrentLookAt);
  
  // If you dont' want the camera to have a smooth panning / turning one, then just replace the camera look at code with:
  //  transform.lookt(CameraGoalLookAt);
  }

Hopefully this works, just ask any questions

SideNote: I suggest pre-setting cameracurrentlookat to the player position so the camera starts the game looking at the player

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

266 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

How to end a cinematic camera cycle 1 Answer

PS3 third person camera orbit script C# 0 Answers

I'm clueless about how to make my script change its position based on how close it is to the wall.. 0 Answers

Changing TPP view to a FPP view after picking up object 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges