How to make an object move in Unity - c#

I just started to use Unity and I found on Youtube a tutorial for my question(making an object move with arrow keys), but I have a problem. My object is constantly moving into the right bottom corner(x is going to minus and z is going to +, y is zero). I did the same thing as the guy in the tutorial.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Move : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.Translate(Input.GetAxis("Horizontal") * Time.deltaTime, 0f, Input.GetAxis("Vertical") * Time.deltaTime);
}
}

Do you have a Rigidbody component added to your object?
If you do, uncheck "Use Gravity" and check "Is Kinematic" in the Unity Editor, inspector window.
That might help solve the problem.

Related

When camera position set to object's, camera doesn't work

I have a camera with all of the default settings with the following C# script below
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Camera1 : MonoBehaviour
{
// Update is called once per frame
void Update()
{
transform.position = GameObject.Find("Thing").transform.position;
}
}
The project runs normally (The player still moves), but I get this
Under the culling mask you need to create a layer for the gameObject your camera is set to follow, otherwise it will not see through it. You can also set its transform position just to be above/in front of it.

Why scripted movement and rotation of sprite gets slow in WebGl build?

I am a beginner Unity developer. I have created this.
The wizard on the top-right corner comes in from outside the screen when the game starts. Then stops and throws the axe. Which eventually hits the detective and causes him to fall.
The wizard and the axe have their scripts. There is no component attached to the wizard. The axe has a rigidbody (gravity scale set to 0) and a box collider attached.
Script of the wizard:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WizardMover : MonoBehaviour
{
public Vector3 move;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
// Wizard stops at at a specific postion
if (transform.localPosition.x > 5.79)
{
transform.localPosition -= move;
}
}
}
And script for the axe:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AxeFly : MonoBehaviour
{
public Vector3 move;
public Vector3 rotate;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.localPosition -= move;
// Axe starts rotating when thrown
if (transform.localPosition.x < 5.80)
{
transform.Rotate(rotate);
}
}
}
Everything works as expected when I play this in Unity. But the speed of scripted movement and rotation of both the wizard and the axe gets significantly slow in the WebGL build. Why is this happening?
Your code is frame-rate dependent!
What this means is on a stronger device which can run with higher FPS (frames per second) your Update method is called more often within the same absolute real time as on a weak device with lower FPS.
So of course if you move with a fixed distance more often within the same time the object will in total travel further and vise versa.
For continous animations in Unity you should always use Time.deltaTime
The interval in seconds from the last frame to the current one (Read Only).
(or sometimes even Time.unscaledDeltaTime in cases where you want to ignore the current Time.timeScale)
which basically converts any value from value per frame into a value per second.
transform.localPosition -= move * Time.deltaTime;
and
transform.Rotate(rotate * Time.deltaTime);
adjust the move and rotate accordingly to the expected absolute value per second.

Unity clamped UI Image is shaking

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.)

Why the gameobject blue axis is facing forward but the gun is on the red axis and not the blue?

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.

Jump OnCollision() Script not working

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.

Categories

Resources