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 LordTravolta · Dec 30, 2016 at 03:25 PM · c#rotationphysicsvector3gravity

How can I bind this script into the state of my camera?

Hi!

I'm working on this project where I'm moving a dot around with gravity. I made a C# script to rotate my camera so the player could see all of the dimensions of my levels | Here's a gif of my current state.

The rotation script is here (all of my scripts are in C#)

 using UnityEngine;
 using System.Collections;
 
 public class CameraRotation : MonoBehaviour {
     
 public GameObject targetObject;
 
      private float targetAngle = 0;
      const float rotationAmount = 2.5f;
      public float rDistance = 1.0f;
      public float rSpeed = 1.5f;
 
      public AudioSource source;
      public AudioClip clip;
      private bool isPlayed;
       
       public void Awake() {
           
           source = GetComponent<AudioSource>();
           isPlayed = false;
       }
      
      void Update()
      {
       
 
          if (Input.GetKeyDown(KeyCode.Q))
          {
              targetAngle -= 90.0f;
              source.Play();
          } else if (Input.GetKeyDown(KeyCode.E)) 
          {
              targetAngle += 90.0f;
              source.Play();
          }
       
          if(targetAngle !=0)
          {
              Rotate();
          }
      }
       
      protected void Rotate()
      {
  
          float step = rSpeed * Time.deltaTime;
          float orbitCircumfrance = 2F * rDistance * Mathf.PI;
          float distanceDegrees = (rSpeed / orbitCircumfrance) * 360;
          float distanceRadians = (rSpeed / orbitCircumfrance) * 2 * Mathf.PI;
          
          if (targetAngle>0)
          {
              transform.RotateAround(targetObject.transform.position, Vector3.up, -rotationAmount);
              targetAngle -= rotationAmount;
          }
          else if(targetAngle <0)
          {
              transform.RotateAround(targetObject.transform.position, Vector3.up, rotationAmount);
              targetAngle += rotationAmount;
          }
      }
 }


If you have any idea how C# works, you can see that this makes the camera have 3+1 possible states where the camera rotates around a nullObject that I can place in the middle of my level.

Here is the gravity changing script:

 using UnityEngine;
 using System.Collections;
 
 public class Gravity : MonoBehaviour
 {
     public Vector3[] gravityList = new Vector3[] { };
     void Update()
     {
         if (gravityList.Length < 5) { Debug.Log("gravity List has to be more or exactly at 5"); return; }
         if (Input.GetKeyDown(KeyCode.Space))
         {
             //Restore Gravity for debugging
             Physics.gravity = gravityList[0];
         }
         if (Input.GetKeyDown(KeyCode.W))
         {
             Physics.gravity = gravityList[1];
         }
         if (Input.GetKeyDown(KeyCode.A))
         {
             Physics.gravity = gravityList[2];
         }
         if (Input.GetKeyDown(KeyCode.S))
         {
             Physics.gravity = gravityList[3];
         }
         if (Input.GetKeyDown(KeyCode.D))
         {
             Physics.gravity = gravityList[4];
         }
     }
 }
 

Now, the question is, how can I bind these scripts in a way that, in whatever camera state, the player-dot will always move to the direction specified? (Press W +9.81 Y) (Press A -9.81 X)

The reason why I'm using gravity instead of just simply moving the player object is that later I will have other objects that should react to the Gravity with the same level of force, just like the player does.

So - what should I do? Code-samples in C# would be amazing. Thanks in advance, people.

Comment
Add comment · Show 2
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 watercat · Apr 19, 2018 at 10:15 PM 0
Share

if you want the player to have the opitions to change button the options is edit Project Settings input if you need add Input button i have options for this button

 (Input.GetButtonDown("name")

 
avatar image watercat · Apr 19, 2018 at 10:33 PM 0
Share

if yor problem is the sky i no needed code just what you need is to camera 1 for the sky and 1 for the add depth smaller number for the skybox camera and for culling mask nathing

for the other camera add depth bigger number skybox change dorn't Clear culling mask Everything or wat ever you want

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by watercat · Apr 19, 2018 at 10:05 PM

wat is the quesstion the rotesin lock to work wat is yor problem ?

i have this code for rotesion bat is js not c#

if you want when he comes in the player

 #pragma strict
 
 var x : float;  
 var y : float; 
 var z : float;
 
 //public var turnSpeed : float = 50f;
 
 function Start () {}
 
 //function OnTriggerStay (other : Collider) { if (other.attachedRigidbody ) other.transform.Rotate(Vector3(x, y, z), -turnSpeed * Time.deltaTime);}
        
 function OnTriggerEnter (other : Collider) {
     if (other.attachedRigidbody)
         other.transform.Rotate(x, y, z); //(Vector3(x, y, z)//, -turnSpeed * Time.deltaTime);
     }
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 Harinezumi · Apr 20, 2018 at 07:49 AM

If I understand it correctly, you would like to move relative to the camera's view direction. To do that, first calculate the desired move vector in local space (where y is always up and x is always right), then use transfom.TransformVector(), with the camera's transform and the move vector.
Something like this:

 Vector3 moveDirection = Vector3.zero;
 if (Input.GetKeyDown(KeyCode.W)) { moveDirection += Vector3.up; }
 if (Input.GetKeyDown(KeyCode.A)) { moveDirection -= Vector3.right; }
 // similar for the rest of the inputs
 
 // was there any input?
 if (moveDirection.sqrMagnitude > 0) {
     // if your movement speed is the same in all directions, do this:
     Vector3 moveVector = moveDirection.Normalize() * moveSpeed;
     // otherwise, apply multipliers when handling input
 
     Transform cameraTransform = Camera.main.transform; // or have a reference to it somehow
     Vector3 relativeMoveVector = cameraTransform.TransformVector(moveVector);
 
     // now you can move relative to the camera direction
 }
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

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

How can I find the location where a ball will land? 0 Answers

Object refuses to collide 0 Answers

How to make a player instantly reach to a change in gravity? 2 Answers

Object rotates normaly while moving, then does zig zags? 0 Answers

How to rotate the player towards a Vector2? 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