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 JayFitz91 · Aug 31, 2017 at 12:17 PM · camerarotationmovement

Problem with camera position

I'm having trouble with my camera, I an freely move it around using the Mouse Orbit script. I have a block of code that will snap the camera behind the player when I press Left Trigger. The problem is, once I let go of the button, the camera reverts back to the location it was previously, but I would like it to stay behind the player. Any help on this is appreciated

I have attached a photo to illustrate what happens, below is the code:

alt text

 if (!playerIsTargeting)
     {
         Debug.Log("here");
 
         x += Input.GetAxis(mouseX) * xSpeed;
         y -= Input.GetAxis(mouseY) * ySpeed;
 
         y = ClampAngle(y, yMinLimit, yMaxLimit);
 
         Quaternion rotation = Quaternion.Euler(y, x, 0);
 
         distance = Mathf.Clamp(distance - Input.GetAxis(mouseY) * speed, distanceMin, distanceMax);
         height = Mathf.Clamp(height - Input.GetAxis(mouseY) * speed, heightMin, heightMax);
 
         Vector3 negDistance = new Vector3(0, 1, -distance);
         Vector3 position = rotation * negDistance + target.position;
 
         transform.rotation = rotation;
         transform.position = position;
     }
     else
     {
         distance = 7;
 
         camPosition = target.position + transform.TransformDirection(new Vector3(0, 2, -distance));
         playerRotation = Quaternion.LookRotation(target.transform.forward);
 
         transform.position = Vector3.Slerp(transform.position, camPosition, 10.0f * Time.deltaTime);
         transform.rotation = Quaternion.Slerp(transform.rotation, playerRotation, 10.0f * Time.deltaTime);
     }

EDIT So I've discovered after playing around why it snaps back to it's original position. The camera is looking at the X and Y values, these do not change when I'm targeting so when I stop, the camera reverts back to these positions, so my question now is, is there a way of changing those X and Y values to the position behind the player?

camera.png (257.5 kB)
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by JayFitz91 · Aug 31, 2017 at 06:06 PM

@Duvic

Thanks for the response but I just managed to figure it out, the X and Y variables were not being changed while Left Trigger was held and thus, the camera position would snap back every time I let go of the Trigger.

So i changed the else clause in my code to keep the X value of the camera equal to the value as the players local Y position, maintaining the position of the camera when leaving the targeting transition

 else
      {
          camPosition = target.position + transform.TransformDirection(new Vector3(0, 1, -distance));
             playerRotation = Quaternion.LookRotation(target.transform.forward);
 
             transform.position = Vector3.Slerp(transform.position, camPosition, 10.0f * Time.deltaTime);
             transform.rotation = Quaternion.Slerp(transform.rotation, playerRotation, 10.0f * Time.deltaTime);
 
             distance = 7;
             x = target.localEulerAngles.y;
             y = 0;
      }
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
avatar image
0

Answer by Duvic · Aug 31, 2017 at 06:00 PM

Yes, there are two easy ways

1) make your variables Public that way you can access them from another script.

2) make a Public function (void) where you are changing the variables in your script and call it from the trigger script.

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
avatar image
0

Answer by ArturoSR · Aug 31, 2017 at 06:09 PM

@JayFitz91 Hello there, you only need to set an initial position variable or say it in other words: "Go behind the player", this variable must be private to avoid undesired changes on it, it could be something like this:

 private Vector3 behindPlayer = new Vector3 (//The position you need);

 private void Start () {
     behindPlayer = camera.position; //This is if the camera it's already behind;
 }

 if (Input.GetKeyUp ("Aim")) {
     camera.position = behindPlayer;
 }

This is just an example, but it's something like that, obviously, you can make the transition more smoothly to avoid the "snap to view X" effect, hopes this helps, cheers.

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

142 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

Related Questions

Smooth Attached Object movement 1 Answer

How to deal with joystick moving and screen swiping colliding? 0 Answers

camera slides and bounces while moving C# 1 Answer

Global movement 0 Answers

How to make my player look in the direction of its movement? 3 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