I have a custom editor and in the scene view I want to be able to execute some code when the UP axis of movement is NOT selected. Is there a way to determine which control is selected before digesting the event in my OnSceneGUI()?
void OnSceneGUI() {
if(/*UP AXIS*/) {
return; // use unity default action
}
// UP Axis not selected do stuff
if (Event.current.isMouse
&& Event.current.button == 0
&& Event.current.type == EventType.MouseDrag)
{
// do some stuff
Event.current.Use(); // digest the event so unity doesn't do default
}
}
Related
I have a scene that has to have the background as solid color.
But when I activate a given use case I need it to have a skybox.
I know I can do this on the editor via:
But I'd like to change it dynamically in C#.
There's no property or field for this in C# default Camera script.
Is it possible to do in Unity?
To change between rendering a skybox and a solid color, set the camera's clearflags
Example Methods:
public void RenderSkybox(Camera targetCamera = null)
{
if (targetCamera == null)
{
//Get reference to main camera if no camera is passed
targetCamera = Camera.main;
}
//set camera to render the skybox
targetCamera.clearFlags = CameraClearFlags.Skybox;
}
public void RenderColor(Color color, Camera targetCamera = null)
{
if (targetCamera == null)
{
//Get reference to main camera if no camera is passed
targetCamera = Camera.main;
}
targetCamera.clearFlags = CameraClearFlags.SolidColor;
targetCamera.backgroundColor = color;
}
Example Usage:
IEnumerator Start()
{
//reference the main camera
var targetCamera = Camera.main;
//Set the camera to render blue as the background color
RenderColor(Color.blue, targetCamera);
//wait two seconds
yield return new WaitForSeconds(2);
//Set the camera to render the skybox
RenderSkybox();
}
Goal
Hello Guys! I am new with unity 3-D . I want to make controls exactly like Mini Golf king . Like when ball stops pointers becomes visible on the screen . On clicking that pointer and dragged its scale changes and points always towards ball.
This is what I want to achieve
MY Approach
Initially according to my knowledge I Have implemented something using ray-cast. In the pointer I placed 3-d pointer as child object of ball . when i touch on the screen I send a ray cast to world from screen that detects Ball if i am touching Ball the i show Pointer and on every point on the screen i am continuously sending ray-casts on the world and changing pointer direction accordingly but i think it is not the good way on performance point
RaycastHit GenerateRayCast(Vector2 position, Camera camera,LayerMask mask) { // GENERATE RAY CAST AT GIVEN DIRECTION IN 3D WORLD
RaycastHit hito;
Ray ray = camera.ScreenPointToRay(position);
Physics.Raycast(ray, out hito, rayLength, mask);
return hito;
}
#endregion
#region Updatemethod
void FixedUpdate () {
if (IsBallInStopPos) { // IF BALL IN STATIC POSITOIN THEN LISTEN FOR TOUCH INPUTS
//if (Input.GetMouseButton(0) && !EventSystem.current.IsPointerOverGameObject())
if (Input.touchCount > 0 && !EventSystem.current.IsPointerOverGameObject()) // CHECK FOR SREEN TOUCH
{
touchInput = Input.touches[0];
// touchInput = Input.mousePosition; // get touch information
hitObject = GenerateRayCast(touchInput.position, Camera.main, mask);
if (touchInput.phase == TouchPhase.Began)
{
if (hitObject.collider.name == "Pointer") { // IF TOUCHED ON POINTER :
if (touchPointSet == false) {
touchPointSet = true;
touchStartPoint = hitObject.point; // SETTING START TOUCH POINT
}
}
}
else if (touchInput.phase == TouchPhase.Moved) {
newScale= Mathf.Clamp (Vector3.Distance(pointerChild.transform.position , hitObject.point),1f,2f);
pointerPivot.transform.localScale = Vector3.one * newScale;// SETTING SCALE ACCORDING TO DRAG VALUE
if (touchPointSet == true) { // WHEN TOUCH DRAGGED AND PREVIOULY POINTER IS TOUCHED ROTATE THE POINTER AT GIVEN DIRECTION
newDirection = new Vector3(hitObject.point.x, transform.position.y, hitObject.point.z);
transform.LookAt(2 * transform.position - newDirection);
canShoot = true;
}
}
}
else if (touchInput.phase == TouchPhase.Ended)
{
if (newScale <= 1f)
return;
if (IsBallInStopPos && canShoot )
{
//shooted = true;
newScale = newScale -1;// AS SCALE VALUE IS BETWEEN 1 AND 2 SO SUBSTACT FROM TO MAKE IT BETWEEN
pointerPivot.gameObject.SetActive(false);
rigidBody.constraints = RigidbodyConstraints.None;
GetComponent<Rigidbody>().AddRelativeForce(new Vector3(0f, 0f, 10000f * newScale));
// Time.timeScale = 2f;
canShoot = false;
IsBallInStopPos = false; // AFTER SHOOT : after the ball is shooted now the ball is not in staticstatic condition
}
}
}
i have a program code on a Player model with Nav Mesh Agent that allows it to walk around the world when clicked but am trying to make it jump and haven't seem to achieve it.
this is my code, don't know what to add or remove
public class WorldInteraction : MonoBehaviour {
NavMeshAgent playerAgent;
// Use this for initialization
void Start () {
playerAgent = GetComponent<NavMeshAgent> (); //instantiate the nav mesh to PlayerAgent
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown (0) && !UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject ()) //condition if the postion is being clicked on a UI is veung clicked
{
GetInteraction (); //call interaction method
}
if (Input.GetMouseButtonDown (1) && !UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject ())
{
transform.Translate (Vector3.up);
}
}
void GetInteraction(){ //this method gets the ray or point clicked and move the player to that point
Ray interactionRay = Camera.main.ScreenPointToRay (Input.mousePosition); //get the point clicked in the world
RaycastHit interactionInfo; //keeps track of the point clicked
if (Physics.Raycast (interactionRay, out interactionInfo, Mathf.Infinity)) //get the point clicked, store it in InteractionInfo and make sure its not out of range by mathf
{
GameObject interactedObject = interactionInfo.collider.gameObject;
if (interactedObject.tag == "Interactable Item") //check if the item point selected is interacrable(cant be move over)
{
interactedObject.GetComponent<Interactable> ().MoveToInteraction (playerAgent); //move playerAgent to the Interactable item, so they could interact(its calling the movetoInteractable method in Interactable class).
} else {
playerAgent.stoppingDistance = 0;
playerAgent.destination = interactionInfo.point; //if its a movable point, player destination is set to that point
}
}
}
}
The NavMeshAgent controls the object in all directions so it will just override your attempt to jump. Make the object with the NavMeshAgent a child of an empty object and just Translate the empty object upwards. Hopefully that helps.
Can I somehow find out is it the first frame the user click on rotater, last frame or middle?
Handles.RotationHandle(...)
or
Handles.PositionHandle(...)
I need to know when the user start rotating/moving and when stop it.
Then you just need to add a variable to save the state. Something like this:
bool rotating;
void Update () {
if (rotating != Handles.RotationHandle(..)) {
rotating = !rotating;
if (rotating) {
//start rotation
} else {
//just stopped
}
} else if (rotating) {
//in rotating
}
}
I'm trying to move a sprite object (the hero) to the current location of the began touch event on the stage. Every time I touch on the stage it reads out the current globalX and globalY coordinates, but the sprite disappears from the stage so I don't know what I am doing wrong.
Here is my code:
private function onTouch(e:TouchEvent):void
{
var touch:Touch = e.getTouch(stage);
if(touch)
{
if(touch.phase == TouchPhase.BEGAN)
{
hero.x += touch.globalX;
hero.y += touch.globalY;
trace("Touched stage at position: " + touch)
}
else if(touch.phase == TouchPhase.ENDED)
{
//The Touch ended (MouseUp)
}
else if(touch.phase == TouchPhase.MOVED)
{
//dragging
}
}
}
Probably your hero sprite is added to Spirte with different size than the object you assigned the touch event. You need a sprite with for example screen size and the hero and the event added to it.
Hope that help :)