I am using the followng script to clamp the UI Image to the player in 2D game, but when the player is moving, the image is shaking a little. What am I doing wrong?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HealthBarClamp : MonoBehaviour
{
public Transform targetToFollow;
Transform thisTransform;
void Start()
{
thisTransform = transform;
}
void Update()
{
thisTransform.position = new Vector3(targetToFollow.position.x,
targetToFollow.position.y + 1.5f, thisTransform.position.z);
}
}
You can try to make the health bar children of your target in the gameObject hierarchy, so that it moves along with it with no need of updating its position in a script. You can then enable/disable the health bar upon your needs if you need it to be seen or not
If you're moving the target object around using physics or in FixedUpdate, then you'll need to move the camera around in FixedUpdate as well. Update happens every frame, fast or slow, whereas FixedUpdate is simulated to happen at consistent intervals to help keep physics calculations from going nuts. If you move the camera in Update and the target in FixedUpdate, then you'll end up with tiny differences that look like shaking.
(Or you can follow the advice given in the comments. It works perfectly fine to make a UI element a child of the moving object, although I personally prefer to do it the way you had it originally because it makes things like "smooth" camera movement easier.)
Related
I tried to create a empty gameobject and put the first gameobject as child but it didn't fix the problem.
Just rotating the object a script is fine but if I'm using LookAt it's not rotating good since the gun is pointing on the red axis and the blue axis is on the body.
Wrong axis
I tried simple LookAt :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LookAT : MonoBehaviour
{
private GameObject target;
// Use this for initialization
void Start()
{
target = GameObject.FindGameObjectWithTag("Enemy");
}
// Update is called once per frame
void Update()
{
transform.LookAt(target.transform);
}
}
Then I tried this : This was a bit better it seems like it was facing with the red axis forward with the gun but not perfect still it didn't target perfect the target I think not sure and not sure if this is a good solution :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LookAT : MonoBehaviour
{
public float speed;
private GameObject target;
// Use this for initialization
void Start()
{
target = GameObject.FindGameObjectWithTag("Enemy");
}
// Update is called once per frame
void Update()
{
Vector3 direction = target.transform.position - transform.position;
Quaternion toRotation = Quaternion.FromToRotation(transform.right, direction);
transform.rotation = Quaternion.Lerp(transform.rotation, toRotation, speed * Time.time);
}
}
The second solution I tried seems the red axis with the gun is facing the target now but the gun and the rings on the base are stuttering and the rings rotating too but they should not rotate I think or they should not be part of the looking on the target.
In this screenshot the whole rings on the base are kind of looking at the target and make the whole turret looks strange. So I'm not sure how ot make this rings and other stuff rotating while only the turret turn object will face the target.
Wrong stuff is rotating and looking facing the target
Looks like the model has axis that don't line up with how you want to use it. Its reccomended that you fix the model, but alternatively you can also add a parent transform on top so you can add some rotation to your model.
Check this out for a more detailed explanation.
To get the two discrete axis of rotation thing I think you are describing at the end of your post you can add two parents as described in the post I linked and apply rotations to each of those parents individually.
I am making a simple VR Tour in Unity, and I want to move the camera to the next sphere (or at all at this point) when I click on a sprite that is hovering in the air. Right now, when I run this code, I get the "Sprite Clicked" message in the console, but the camera doesn't move at all. Any help is greatly appreciated!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DebugOnClick : MonoBehaviour {
// Use this for initialization
void Start () {
}
void OnMouseDown () {
Debug.Log("Sprite Clicked");
Camera.main.transform.position = new Vector3(5.0f, 5.0f, 5.0f);
}
}
It doesn't let me comment (not enough rep) so I must pop this in as an answer.
If you're using some given VR scripts already, it could be possible that they're syncing the camera position constantly to where they should be relative to the player's headset position.
Meaning that after this move via script, the camera will be moved back instantly.
If that is the case, you need to move the player avatar themselves to the correct position rather than the camera.
I'm trying to understand how to get the closest to a destination with Unity's navigation system when a Nav Mesh Obstacle is in the way.
I have this example with a Capsule obstacle (with Carve on) and a capsule agent managed with a script. It seems to work okish, but when I "click" on the obstacle to set the destination of the agent (to a point inside the carved area), the agent moves to another location around the obstacle.
How can I make the agent go to the closest point around the obstacle or to the closest point to the selected destination (that is inside the area of the obstacle)?
Script to move the agent
using UnityEngine;
using UnityEngine.AI;
public class CapsuleMovement : MonoBehaviour {
NavMeshAgent agent;
public NavMeshPathStatus partial;
void Start() {
agent = GetComponent<NavMeshAgent>();
}
void Update() {
if (Input.GetMouseButtonDown(0)) {
RaycastHit hit;
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 100)) {
agent.destination = hit.point;
}
}
}
}
Take the center of the clicked objects position and move it slightly towards the moving unit/player, then use that position as your coordinate instead of the direct mouse click.
If that doesn't work.
This is not a guaranteed answer as that is hard to give in a situation such as this, but I hope this brings you further.
I suspect that it may be the coordinate you click, which something is not working out as it should with. Try spawn a "ClickObject" which can just be a coloured sphere, at the position of the mouse click. That way you can confirm where the click actually is happening.
Here is furthermore 2 methods that may come in handy when working with NavMeshes and positioning.
You could try use SamplePosition.
https://docs.unity3d.com/540/Documentation/ScriptReference/NavMesh.SamplePosition.html
Finds the closest point on NavMesh within specified range.
Perhaps also FindClosestEdge
https://docs.unity3d.com/530/Documentation/ScriptReference/NavMesh.FindClosestEdge.html
Locate the closest NavMesh edge from a point on the NavMesh.
basically, I'm trying to make a game that is based off automatically hopping as soon as the ground is hit(the Controller I'm using is the pre-made one that can be imported).In order to do this, I removed the jump function from the controller script and added a script called "Cube" which reads as the following:
using UnityEngine;
using System.Collections;
public class Cube : MonoBehaviour {
void OnCollisionStay (Collision col)
{
if (col.gameObject.name == "Blue") {
Rigidbody rig;
rig= GetComponent<Rigidbody> ();
rig.velocity = new Vector3 (0, 8, 0);
print ("collison detected");
}
}
}
after doing this, I expected a controllable character which jumps as soon as you hit the ground because of the "OnCollisionStay()" trigger. However, instead I get a rapid jump that happens even when I'm in the air which looks like this:
https://youtu.be/ILtRac_RgLg
First of all, undo everything modification you performed to the RigidbodyFirstPersonController script. If possible, delete it and re-import a clean one from Unity.
Select your RigidBodyFPSController GameObject, Look at the RigidbodyFirstPersonController script attached to it in the Editor. Under it there is a setting called Advanced Settings. Under Advanced Settings, there is variable called Shell Offset. Change Shell Offset from its default value of 0 to 0.5. Play again and this problem should be gone. If that didn't work, bump it up more. This should solve your problem.
You are not in the air as the gravity is effecting on it. Whenever you are exiting the collision your y-axis upward velocity is not working and gravity force is taking place downward.
Disable gravity in FPSController's Rigidbody component if you don't want to use gravity.
So I have a player. It has hands attached made from two rectangles.(I attached .GIF)
So I need it to move how the player moves right stick on joystick. I made some controls, but I think they are poor, and inaccurate. I believe, it's possible to make it better. Hope I will find some help here. What I made up is an object (invisible, visible only for describing problem, debuging purposes) that moves on axis of joystick. Then there are player, he has hands. On the lower hand lower there is script attached. Before I had made spring, but I didn't liked result. Script:
public GameObject Target;
void Update () {
if (Input.GetKey (KeyCode.J)) {
transform.position = Vector3.MoveTowards(transform.position, Target.transform.position, 1);
}
So the question is, how to upgrade this system hands control system? I would like it to be more physical, because I'm making a volleyball game.