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.
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")
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
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);
}
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
}