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 /
  • Help Room /
avatar image
0
Question by CorvosKK · Nov 04, 2016 at 02:46 PM · cameratransformaxis

How do I exclude an axis from 'transform.localPosition'?

I'm working on extending the Space Shooter Tutorial with some of my own custom elements. Within the GameOver state I'm trying to add a camera shake to my mainCamera. Below is all of the relevant code:

 public float shake;
 public float shakeStrength;
 public Camera mainCamera;
 private float shakeDecline;

 void Update () {
     if (shake > 0.0f) {
         mainCamera.transform.localPosition = Random.insideUnitSphere * shakeStrength;
         shake -= Time.deltaTime * shakeDecline;
     } else {
         shake = 0.0f;
     }
 }

 public void GameOver () {
     gameOverText.text = "GAME OVER";
     shake = 1;
     hudFrame.enabled = false;
     hudFrameGameOver.enabled = true;
     gameOver = true;
 }

The transform code that I found works great except for one little problem.

The camera's position starts at (0, 10, 5). The position.y is far enough away to capture all of the game elements. When I run Random.insideUnitSphere, it affects all of the axis, which brings the camera's position on the Y axis close enough to chop off and hide other game objects from view.

Is there a way that I can still run Random.insideUnitSphere on X and Z, while leaving the Y value permanently at '10'?

Thank you!

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 NoseKills · Nov 04, 2016 at 03:48 PM

Do you mean the camera's parent object is at (0,10,5)? InsideUnitSphere returns vectors where all the values are in the range of -1 to 1, and you are setting that straight as the localPosition of the camera. If localPosition changes from (0,10,5) to for example (-0.6, -0.6, -0.6) that's a big change and might be the cause of your problems. But if the parent object is at (0,10,5) and you move the camera around the center of its parent (0,0,0) to (0.6, 0.6, 0.6), that could work.

You can "ignore" one axis from the random position like this

 var randomOffset = Random.insideUnitSphere * shakeStrength;
 randomOffset.y = 0;
 mainCamera.transform.localPosition = randomOffset;

but another way to do this without parenting would be to store the position of the camera when the shake starts and then, when shaking, set the position to be the original position + random offset.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Why does my transform.lookat not work? 1 Answer

Transform.Rotate x and y rotate z, where z is 0 0 Answers

Limit game object to camera view 0 Answers

2D Player bounds problem based on moving camera 0 Answers

Hard coded rotation of child results in weird arc rotation when parent is rotated? 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