Adding movement to 3D text - unity3d

Attempting to make text move with the left and right arrows.
I placed a 3D text component and added a c# script for movement.
I attached the script to the text but it doesn't want to move.
Whats the trick? It works when using a cube but thats not what i'm trying to do.
void Update ()
{
if (Input.GetAxis("Horizontal") != 0)
{
transform.Translate(new Vector3(Input.GetAxis("Horizontal") * speed * Time.deltaTime, 0,0));
}
}

Ok figured it out but had to add a box collider and set it correctly

Related

rigid body moves by addForce but the object itself doesn't

I'm following this video https://www.youtube.com/watch?v=THnivyG0Mvo and This is my shoot function
void Shoot()
{
muzzleFlash.Play();
shootingSound.Play();
RaycastHit hit;
if( Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
{
//Debug.Log(hit.transform.name);
Enemy1 target = hit.transform.GetComponent<Enemy1>();
if (target != null)
{
target.TakeDamage(damage);
}
if(hit.rigidbody != null)
{
Debug.Log("******************");
hit.rigidbody.AddForce( - hit.normal * impactForce);
}
GameObject impactGo = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(impactGo, 0.3f);
}
}
Rigidbody added to target:
Barrel component:
enter image description here
This function is in my Rifle.cs script which is being added to a rifle object. Everything works fine. But, when I hit an object which has a Rigidbody, it doesn't move but I can see in the scene that the Rigidbody is moving when I hit it many times. The Rigidbody of the target is set to 'Use Gravity' and 'Is Kinematic' is not checked. What am I doing wrong ?
Probably the force you are adding is too small, so it needs a lot of shots to make some effect, as #Horothenic says, try to increase the value of the impactForce Variable. Look if the rigidbody, the mesh renderer, and the colliders are attached to the same object in the scene. The title of your question suggests your rigidbody is moving but your render doesn't change.
Give the force a ForceMode. Since you want the object to look shot, I would recommend using impulse and make sure the force is bigger than the objects mass by a big factor.
if(hit.rigidbody != null)
{
Debug.Log("******************");
hit.rigidbody.AddForce((-hit.normal * impactForce), ForceMode.Impulse);
}
See this page for more info. Force Modes in Unity
Try removing the Animator completely, the animations have an option to write defaults, if the movement was edited on animation it misbehaves because it wants to set the default.
After I have done a lot of google search. This answered my question https://forum.unity.com/threads/mesh-renderer-does-not-move-with-parent-rigid-body.204700/. So What I needed to do it to turn off (unchecked) static in the top right of the inspector for the object.
enter image description here

move object and it's children out of camera in unity2d

I have made a gameobject together with some children gameobject to represent the information to show up when specific circumstances occurred.
I have already ajusted the position of the information gameobject(together with its' children) in the cameraarea. The thing is that I want to move the gameobject(together with its' children) out of the camera, maybe on top or maybe on left. Following is the scratch to demonstrate the position I want to put it:
So that I could move the information gameobject and its' children (Red box) with some movement effect when needed, I have no problem with moving it back but could find an elegant way to move it out of the camera when the game started.
Mostly because I don't know how to calculate the position out of the camera.
Maybe find the upper border of the camera and the size of the gameobject and its children?
I know I could do this by maybe add a marker gameobject to represent the downer border of the information gameobject, and move it until it's not visible, but is there a more elegant way?
Any ideas?
For this one, I would use the following trick: use any method (animation, coroutine, Update method...) to move your item out of screen the way you desire. Then you can use the OnBecameInvisible event which is called when the item does not need to be rendered on any camera anymore. The event will there be used to detect that the parent object moved out of screen, and that you want to stop the current movement. You then just need to define in this event that you want to stop the current moving behavior, and you will be done.
void OnBecameInvisible() {
// Stop moving coroutine, moving in Update or current animation.
}
There are probably more elegant ways of doing it as you said, but I think this method should be enough for what you want to achieve.
It took me time, but I found this way for you, attach this script to your gameObject:
public Renderer rend;
//drag the camera to the script in the inspector
public Camera camera1;
Vector3 bottomleft;
Vector3 topright;
void Start()
{
rend = GetComponent<Renderer>();
//the top-right point of the camera bounds
topright= camera1.ViewportToWorldPoint(new Vector3(0, 0, transform.position.z));
//the bottom-left point of the camera bounds
bottomleft = camera1.ViewportToWorldPoint(new Vector3(1, 1, transform.position.z));
StartCoroutine(MoveUp());
}
IEnumerator MoveUp()
{
//while the position and the height are lower that the Y of top right
while (transform.position.y + rend.bounds.size.y < topright.y)
{
//move the object to the new position (move it up)
transform.position = new Vector3(transform.position.x, transform.position.y + .01f, transform.position.z);
//and wait for 1/100 of a second
yield return new WaitForSecondsRealtime(.001f);
}
}
you can play with the WaitForSecondsRealtime value to change the velocity of moving.

Why is my character passing through the object even though I have configured box colliders and rigidbody for them?

Check inspection for: Blocking wall
Check inspection for: character
It still passes through the blocking wall.
In Script in character:
if (Input.GetKey(KeyCode.UpArrow))
{
transform.Translate(Vector3.forward * 15 * Time.deltaTime);
}
if (Input.GetKey(KeyCode.DownArrow))
{
transform.Translate(-Vector3.forward * 15 * Time.deltaTime);
}
You components are correctly set: one rigidbody at the character and colliders in both the character and the wall.
As said by Greg: if you want to move your character in a more realistic way(based in the unity physics) you may want to change your code to use AddForce instead. However, its not mandatory. If you're doing something like a space invaders game, move the character the way you are doing is ok.
Another thing you should consider is the possibility of the wall collider be too thin. If that is the case, unity could not be able to detect the collision properly.
If your object is moving quickly you might want to change Collision Detection: Descrete to a Continuous one in the inspector. Also, you might want to modify the rigidbody velocity for movement instead.
Also. Make sure you're not using 3d objects and 2d objects combined. They don't match. A 2D object wont collide with a 3D object.
RigidBody rb;
void Start() {
rb = GetComponent<RigidBody>();
}
void Update() {
if (Input.GetKey(KeyCode.UpArrow))
{
rb.velocity = Vector3.forward * 15 * Time.deltaTime;
}
if (Input.GetKey(KeyCode.DownArrow))
{
rb.velocity = -Vector3.forward * 15 * Time.deltaTime;
}
}
Disclaimer: Might need to adjust the values a bit!
Use the rigidbody.AddForce function to move your player/object.
Translating the transform is more like teleporting than moving. So your object teleports to the other side of the wall with transform.Translate.
Check this link for the rigidbody.AddForce movement method: Unity - Moving the player
Update 1
You should also set the collision detection to continuous instead of discrete.
First of all. You do need Rigid body component on your wall at all. Collider itself will cause the collision.
For Unity physics to work properly you should not modify transform position directly but move your character by using forces.
Try this:
void FixedUpdate() {
Rigidbody rb = this.GetComponent<Rigidbody>();
if (Input.GetKey(KeyCode.UpArrow))
{
rb.AddForce(Vector3.forward * 15f);
}
if (Input.GetKey(KeyCode.DownArrow))
{
rb.AddForce(-Vector3.forward * 15f);
}
}

Moving something rotated on a custom pivot Unity

I've created an arm with a custom pivot in Unity which is essentially supposed to point wherever the mouse is pointing, regardless of the orientation of the player. Now, this arm looks weird when pointed to the side opposite the one it was drawn at, so I use SpriteRenderer.flipY = true to flip the sprite and make it look normal. I also have a weapon at the end of the arm, which is mostly fine as well. Now the problem is that I have a "FirePoint" at the end of the barrel of the weapon, and when the sprite gets flipped the position of it doesn't change, which affects particles and shooting position. Essentially, all that has to happen is that the Y position of the FirePoint needs to become negative, but Unity seems to think that I want the position change to be global, whereas I just want it to be local so that it can work with whatever rotation the arm is at. I've attempted this:
if (rotZ > 40 || rotZ < -40) {
rend.flipY = true;
firePoint.position = new Vector3(firePoint.position.x, firePoint.position.y * -1, firePoint.position.z);
} else {
rend.flipY = false;
firePoint.position = new Vector3(firePoint.position.x, firePoint.position.y * -1, firePoint.position.z);
}
But this works on a global basis rather than the local one that I need. Any help would be much appreciated, and I hope that I've provided enough information for this to reach a conclusive result. Please notify me should you need anything more. Thank you in advance, and have a nice day!
You can use RotateAround() to get desired behaviour instead of flipping stuff around. Here is sample code:
public class ExampleClass : MonoBehaviour
{
public Transform pivotTransform; // need to assign in inspector
void Update()
{
transform.RotateAround(pivotTransform.position, Vector3.up, 20 * Time.deltaTime);
}
}

How to make wall push the player in unity3d?

How to make wall push the player ?
i had tried to use transform.translate, however i found out directly manipulating transform
component of an object ignores physic, and someone suggest me to use force instead.
however, when i use force, the wall
just stop when it hit my player, as if my player can't be moved.
Below are my code.
using UnityEngine;
using System.Collections;
public class left : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
rigidbody.AddForce (-40 * Time.deltaTime, 0, 0);
if (transform.position.x < -14) {
transform.position = new Vector3(15,15,908);
}
}
}
Increase the mass of the wall or reduce the mass if the player
First of all, you should put a collider on both the box and the player. Then create a script on the player. Something that would tell it that when the box collided with the player, it would move to the destined position, depending on the force applied.
something like
OncollideO(){
player.transform.position.x -= Time.deltaTime * 1;
}
you do not have to set the vectors manually to be able to move them.
you may also use left, right, up, and down in moving gameobjects.
might as well as tell you to research about LERP which may be able to help you in coding it.