Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 sleeperkiller06 · Apr 04, 2011 at 12:22 AM · cameramousecontrol

Camera Control Help

I have this PlayerCameraControl script that controls the camera when you move the character and when you press and hold the right mouse button. When you press the right mouse button, you than can rotate the camera around the character through the mouse. The problem I'm having is that when you press the right mouse button, the camera snaps to the same place every time. I want the camera to always start behind the player no matter the direction. So if my character is facing down the negative x, I want my camera's starting position when I press it to be right behind him. Can someone help me out.

//Camera That is used for player var mainCameraTransform : Transform; // The target we are following var target : Transform; // The distance in the x-z plane to the target var distance = 10.0; // the height we want the camera to be above the target var height = 5.0; // How much we var heightDamping = 2.0; var rotationDamping = 3.0;

//place holder for x and y postion of mouse var x : float; var y : float;

//The rotation speed of the x and y for the manual camera rotationDamping var xSpeed : float = 250.0; var ySpeed : float = 120.0;

//boolean for mouse click var buttonPressed: boolean = false;

function Start(){

 if(mainCameraTransform == null)
     mainCameraTransform = Camera.main.transform;

 target = transform;

}

function Update(){

 if(Input.GetMouseButtonDown(1)){
     buttonPressed = true;
 }
 if(Input.GetMouseButtonUp(1)){
     buttonPressed = false;
 }

}

function LateUpdate () { // Early out if we don't have a target if (!target) return;

 if(buttonPressed){

     var startDirection = mainCameraTransform.position - target.position;

     x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
     y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;

     var tempRotation = Quaternion.Euler(y, x, 0);
     //var tempPosition = tempRotation * Vector3(0.0,height,distance)  + target.position;
     var tempPosition = tempRotation * Vector3(0.0,0.0,distance) + target.transform.position;

     mainCameraTransform.transform.rotation = tempRotation;
     mainCameraTransform.transform.position = tempPosition;
     mainCameraTransform.transform.LookAt(target);

 }
 else{

     x = 0;
     y = 0;

     // Calculate the current rotation angles
     wantedRotationAngle = target.eulerAngles.y;
     wantedHeight = target.position.y + height;

     currentRotationAngle = mainCameraTransform.transform.eulerAngles.y;
     currentHeight = mainCameraTransform.transform.position.y;

     // Damp the rotation around the y-axis
     currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);

     // Damp the height
     currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);

     // Convert the angle into a rotation
     currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);

     // Set the position of the camera on the x-z plane to:
     // distance meters behind the target
     mainCameraTransform.transform.position = target.position;
     mainCameraTransform.transform.position -= currentRotation * Vector3.forward * distance;

     // Set the height of the camera
     mainCameraTransform.transform.position.y = currentHeight;

     // Always look at the target
     mainCameraTransform.transform.LookAt (target);

 }

}

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

0 Replies

· Add your reply
  • Sort: 

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

No one has followed this question yet.

Related Questions

How to make camera position relative to a specific target. 1 Answer

how to find mouse look 1 Answer

Camera.main.ScreenToWorldPoint works within Unity editor, but not in builds 0 Answers

Age of empires Mouse and Camera controls (SOLVED) 2 Answers

Freeze Camera Rotation that is attached to the object in certain moments 0 Answers


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