Okay, so I have a click to move game and a panel in which I do not want my player to move towards. However the panel is disabled at first (for an effect I'm doing) until the user clicks on a button to SetActive the panel. I used currentSelectedGameObject to block my player going to towards the panel but it isn't working, maybe because the panel was disabled in the first place, I not sure just spitting out ideas. Hopeful someone can help me.
using UnityEngine.EventSystems;
public GameObject currentSelectedGameObject;
public void Update () {
if (Input.GetMouseButtonDown (0)) {
if (EventSystem.current.currentSelectedGameObject)
return;
Vector3 mousePosition = Input.mousePosition;
mousePosition.z = 10; // distance from the camera
target = Camera.main.ScreenToWorldPoint (mousePosition);
target.z = transform.position.z;
}
transform.position = Vector3.MoveTowards (transform.position, target, speed * Time.fixedDeltaTime);
}
Thank you. :)
Use the button to setactive the panel and if not , you are not using the public GameObject currentSelectedGameObject variable in your code , do this :
if (EventSystem.current.currentSelectedGameObject == currentSelectedGameObject) // now this is your variable that you declared being used in the if statement
I cant remember correctly but mb its like this EventSystem.current.currentSelectedGameObject.name
Related
I am working on a third person parkour game with new input system and cinemachine(Freelook). You move with the right joystick and look around using right joystick(Cinemachine Input provider). For doing tricks I am using button with one modifier, you press R2 and move the right joystick in different directions for different tricks. But when I move the right joystick for a trick cinemachine rotates the camera.
Is a way to stop cinemachine from rotating the camera when other buttons are pressed.
Thank you in advance, Hemanth
You can simply disable the component.
public CinemachineFreeLook freeLook;
private void Lock() => freeLook.enabled = false;
Another way is to set the Mouse Axis Speed to zero. In this method, with the help of a tweener, you can gently disable the movement of the mouse.
private void Lock()
{
DOVirtual.Float(freeLook.m_XAxis.m_MaxSpeed, 0f, 1f, value => freeLook.m_XAxis.m_MaxSpeed = value);
DOVirtual.Float(freeLook.m_YAxis.m_MaxSpeed, 0f, 1f, value => freeLook.m_YAxis.m_MaxSpeed = value);
}
freeLook.m_YAxis.m_MaxSpeed = 0;
freeLook.m_XAxis.m_MaxSpeed = 0;
You can just set the values to 0
you can probably remove the component that will probably help
Every time I start my game the character starts to rise and fly above the ground. I don't understand how to fix it. I've tried to move the ground up but nothing's changed. 2D game if it matters.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player_Controller: MonoBehaviour
{
public float speed = 0.1f;
private void Update()
{
transform.position += new Vector3(speed, 0, 0) * Input.GetAxis("Horizontal");
transform.position += new Vector3(0, speed, 0) * Input.GetAxis("Vertical");
if(Input.GetAxis("Horizontal") < 0)//если влево
{
GetComponent<SpriteRenderer>().flipX = true;
}
if(Input.GetAxis("Horizontal") > 0)//если вправо
{
GetComponent<SpriteRenderer>().flipX = false;
}
}
}
You should be able to just add the Rigidbody Component to your Character GameObject, then enable gravity in the component and your character should just fall on the ground.
EDIT: If you find your character in the Unity Hierarchy (left hand side of the screen by default) and then to the inspector window (right hand side of the screen by default. At the bottom of the inspector, there should be an ‘Add Component’ button. Click that button and then search ‘Rigidbody’ in the search field that appears. Add the Rigidbody Component. By default, the enable gravity option should be ticked but if it isn’t, just make sure you tick that box. When you run your game with gravity enabled, gravity should act on the GameObject with the ‘Rigidbody’ Component, pulling it to the ground
Also, make sure that you follow the same process as above to add the ‘Mesh Collider’ Component to your platform/ground - this will make your ground a ‘Physical’ object which will prevent your character falling straight through it
Hope this helps - let me know if it doesn’t work
I have been following along with a course. In this course a GameObject called "Path (0)" is added, this GameObject then has three sub-GameObjects ("Waypoint (0/1/2)"). In the tutorial, the guy drags the parent GameObject "Path (0)" into a folder called Paths in the Project panel to create a prefab. This prefab is shown with a handle to expand it. He then expands this prefab to see the sub-elements which can be dragged into the Inspector, as shown below (expand button highlighted with red box)
When I perform these operations, the Project panel does not have an expand button on the "Path" prefab (see below)
Why does my prefab object in the Project panel not have these expansion buttons?
Basically, what he does is he creates a script component with enemy prefab.(attached bellow). and the childobject "waypoints" are marks in scene where enemy will move. I uploaded a part of tutorial on youtube, which i will delete once the problem is solved. if youtube dosent delete it. Please help i am half through the tutorial.
https://youtu.be/NGpaHSmktic
public class EnemyPathing : MonoBehaviour {
[SerializeField] List waypoints;
[SerializeField] float moveSpeed = 2f;
int waypointIndex = 0;
void Start()
{
transform.position = waypoints[waypointIndex].transform.position;
}
// Update is called once per frame
void Update()
{
Move();
}
private void Move()
{
if (waypointIndex < -waypoints.Count - 1)
{
var targetPosition = waypoints[waypointIndex].transform.position; // this is where we go to
var movementThisFrame = moveSpeed * Time.deltaTime; // this is how fast we want to go to
transform.position = Vector2.MoveTowards(transform.position, targetPosition, movementThisFrame);
if (transform.position == targetPosition)
{
waypointIndex++;
}
}
else
{
Destroy(gameObject);
}
}
It must be because you don't have the same unity version.
Simply double click on Path0 and it will open it. In the Hierarchy tab, you will see the asset and its elements.
If you drag back Path0 to the scene, its children will appear too.
It's because you're not supposed to drag a child asset away from its parent.
I don't know how it is used in your tutorial, but if needed, you can make a child prefab too by dragging it to the folder (After double clicking on Path0).
Edit :
I don't really know what you can do to have the exact same situation, but here is another way :
-create a tag 'waypoint' and set this tag for Waypoint0, Waypoint1 and Waypoint2 (in your asset, or before creating it).
-In the Start method of your EnemyPathing.cs add this :
this.waypoints = new List<Transform>();
foreach(GameObject g in GameObject.FindGameObjectsWithTag("waypoint"))
{
waypoints.Add(g.transform);
}
So, it will automatically add your waypoints, even if you have more or less than 3 in your scene.
I have 2 Spheres in my scene. I want to be able to drag and drop my mouse from one sphere to the other, and have an indicator (a straight line for example) while dragging. After releasing the mouse button, I want to store in the first sphere the other sphere (as GameObject).
I need this in UnityScript, but I can accept C# ideas.
So far I have thought about onMouseDown event on the first Sphere, and then onMouseEnter to the other sphere, I'll store the data in some global variable (which I donno how to do yet) and in case of onMouseExit I'll just put the global variable as null.
Then onMouseUp on the first Sphere I'll store the global variable in the pressed object (the sphere).
Any tips and tricks on how to do it?
Assumptions/Setup
You're working in a 2D worldspace; the logic for dragging the object for this part of the solution would need changing.
Setting parent after click drag, referred to setting the object you didn't click to be the child of the parent you did click on.
The camera should be an orthographic camera; using a perspective camera will cause the dragging to not align with where you think it should be.
In order to make the dragging work, I created a quad that was used as the 'background' to the 2D scene, and put that on a new layer called 'background'. Then setup the layermask field to only use the background layer.
Create and setup a material for the line renderer, (I'm using a Particles/Additive shader for the above example), and for the parameters of the line renderer I'm using Start Width: 0.75, End Width: 0, and make sure Use World Space is ticked.
Explanation
Firstly setup the otherSphere field to be pointing to the other sphere in the scene. OnMouseDown and OnMouseUp toggle the mouseDown bool, that is used to determine if the line renderer should be updated. These are also used to turn on/off the line renderer, and set/remove the parent transform of the two spheres.
To get the position to be dragged to, I'm getting a ray based off of the mouse position on screen, using the method ScreenPointToRay. If you wanted to create add smoothing to this drag functionality, you should enable the if (...) else statement at the bottom, and set the lerpTime to a value (0.25 worked well for me).
Note; when you release the mouse, the parent is immediately set, as such both objects end up getting dragged, but the child will return to it's former position anyway.
[RequireComponent(typeof(LineRenderer))]
public class ConnectedSphere : MonoBehaviour
{
[SerializeField]
private Transform m_otherSphere;
[SerializeField]
private float m_lerpTime;
[SerializeField]
private LayerMask m_layerMask;
private LineRenderer m_lineRenderer;
private bool m_mouseDown;
private Vector3 m_position;
private RaycastHit m_hit;
public void Start()
{
m_lineRenderer = GetComponent<LineRenderer>();
m_lineRenderer.enabled = false;
m_position = transform.position;
}
public void OnMouseDown()
{
//Un-parent objects
transform.parent = null;
m_otherSphere.parent = null;
m_mouseDown = true;
m_lineRenderer.enabled = true;
}
public void OnMouseUp()
{
//Parent other object
m_otherSphere.parent = transform;
m_mouseDown = false;
m_lineRenderer.enabled = false;
}
public void Update()
{
//Update line renderer and target position whilst mouse down
if (m_mouseDown)
{
//Set line renderer
m_lineRenderer.SetPosition(0, transform.position);
m_lineRenderer.SetPosition(1, m_otherSphere.position);
//Get mouse world position
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out m_hit, m_layerMask))
{
m_position.x = m_hit.point.x;
m_position.y = m_hit.point.y;
}
}
//Set position (2D world space)
//if (m_lerpTime == 0f)
transform.position = m_position;
//else
//transform.position = Vector3.Lerp(transform.position, m_position, Time.deltaTime / m_lerpTime);
}
}
Hope this helped someone.
I have a problem with Unity 4.0.0. When I play the AngryBots project or even when I make my own char with a simple walking script, it automatically walks without pressing any button.
I tried to uninstall Unity, I tried it with an older version of Unity, I even deleted it from the registry, but I still get the same behaviour. Why does that happen? Is it a bug, or am I doing something wrong?
var speed = 6.0;
var jumpSpeed = 8.0;
var gravity = 20.0;
private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
function FixedUpdate() {
if (grounded) {
// We are grounded, so recalculate movedirection directly from axes
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
} // Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
var controller : CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags & CollisionFlags.CollidedBelow) != 0;
}
#script RequireComponent(CharacterController)
I am using this script for all my walking chars and it works, but now its walking automatically.
The issue is something is providing movement axis input.
Generally it will be another device such as joystick etc. First step will be to unplug any other input devices you might have to test (Isolate the problem - then fix as required)
Another thing that may be causing it: I had the same issue but no other input devices plugged in. I found that a driver called VJoy virtual joystick (Installed as part of FreetrackNoIR) was setting movement axis values to 1 and -1 for each axis, thus resulting in the same problem.
Disabling this driver (Control panel, device manager, Human Interface devices) fixed it for me!
Good luck.
You have to use an "Animation" component, not an "Animator" one. (You can assign this by clicking on "Add Component", "Miscellaneous" and "Animation." This will contain the "Play Automatically" button you need.
I had this same thing happen to me. My characters would go UP and LEFT when I would move my mouse off of the Game window while in the editor and in my builds.
Go to "Edit, Project Settings, Input" and verify that your Horizontal and Vertical are set accordingly. Once I deleted the second set of inputs for Horizonal and Vertical in the InputManager, it stopped doing that.