script causing odd scaling behaviour on child object in unity - c#

I made a small turret object and a target object that moves about my scene.
In the turret's script I make them aim at the object.
using UnityEngine;
using System.Collections;
public class indivMover : MonoBehaviour
{
public GameObject t1;
public GameObject target;
// Update is called once per frame
void Update ()
{
t1.transform.LookAt(target.transform);
}
}
this worked fine until I parented many of the turrets to a larger object so I could move them around as one. Now they all distort as they point away from their starting position. Why is this happening?

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.

How can I generate terrain rather than an object on drag and drop in Unity?

Background
I would like to know how to write a code that a player can generate terrain by drag and drop in Unity3D version 2020.3.
What I tried so far
I can create terrain before execution for unchangeable background and land during the executing time.
And the case of objects(prefabs), I confirmed that a player could generate a new object at selected positions.
code which was attached to a gameobject
ClickSphere.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ClickSphere : MonoBehaviour
{
public GameObject prefab;
private Vector3 mousePosition;
void Update()
{
if (Input.GetMouseButtonDown(0)) //the case of drag -> OnMouseDrag()
{
mousePosition = Input.mousePosition;
mousePosition.z = 10.0f;
Instantiate(prefab, Camera.main.ScreenToWorldPoint(mousePosition),Quaternion.identity);
}
}
}
How can I attach a code to terrain and how can I approach for terrain compared to object generation?
If you are trying to do this in a 3D project, i suggest you place a plane(on which you can place the objects). And than cast a ray from the camera(in the cameras orientation http://docs.unity3d.com/ScriptReference/Physics.Raycast.html) and depending on the object that the ray hits, intantiate the object you want to place at the height at which the ray hit. So you can generate the terain you want and than, when the user needs to place stuff, it wont overlap because you know the point of intersection between the ray and whatever terain you initialy generated.

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

How to create particles in Unity at a point of impact

So I'm working on a Pong project as a learning tool to help me get used to Unity as a development studio. What I want to do is create a Particle System every time the ball hits either paddle. So I put a particle system as a child under each paddle, and made it so it called that particle system every time the ball made contact with the paddle in C#. It doesn't work, and I want to make it so it calls the particle system at the point of impact between the ball and the paddle. Here's the code I currently have:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ExplosionScript : MonoBehaviour {
public GameObject explosionParticle;
void OnCollisionEnter2D (Collision2D coll) {
if (coll.collider.CompareTag ("Player")) {
Explode ();
}
}
void Explode () {
Instantiate (explosionParticle, Vector3.zero, Quaternion.identity);
}
}
This code is attached to the ball GameObject.
Try
explosionParticle.Play(); after instantiating.
typically I create a separate ParticleSystem gameobject and not a child so I can prefab or modify it. Then I declare
public ParticleSystem explosion; then attach the ParticleSystem gameobject to the ball explosion property.
In code I would do just this
Instantiate(explosion, transform.position, Quaternion.identity);
explosion.Play();

Why is there no camera attached, even if I've attached it?

So, i want to use camera.ViewportToWorldPoint() to show the bottom center bounds of my screen. So, I created a script, add that component to my object that need it.
using UnityEngine;
using System.Collections;
public class PathMovement : MonoBehaviour {
public Camera cam;
private Vector3 bound;
void Awake () {
cam = GetComponent<Camera> ();
}
void Start(){
bound = cam.ViewportToWorldPoint (new Vector3 (0f, 0.5f));
Debug.Log (bound);
}
}
and then, I attach the MainCamera via the GUI
And then, when I run it, there's still an error says :
MissingComponentException: There is no 'Camera' attached to the "RiverPath" game object, but a >script is trying to access it.
You probably need to add a Camera to the game object "RiverPath". Or your script needs to check if >the component is attached before using it.
UnityEngine.Camera.ViewportToWorldPoint (Vector3 position) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineCamera.gen.cs:408)
PathMovement.Start () (at Assets/Scripts/PathMovement.cs:21)
This is quite weird since I've attached the main camera, but somehow unity didn't detect that. I also have tried to put the cam = GetComponent<Camera>(); on Awake() as well as Start(), but none work. :(
Btw, I am making a mobile application on android. And using unity 5.
Is there any way to do it properly??
Thanks.
You're getting the MissingComponentException as there is no Camera attached to that particular GameObject (see the description of the Exception).
What you need to know is that GetComponent only looks for that Component in the current GameObject. Depending on your GameObject hierarchy you may want to use GetComponentInParent or GetComponentInChildren instead.
Or you could also attach the Camera using the Editor (drag and drop). This may not work if you instantiate that Prefab during runtime, but should do just fine when its static in the scene.

Categories

Resources