Quantcast
Channel: Questions in topic: "independent"
Viewing all 102 articles
Browse latest View live

Como posso mover GameObjects de mesmo script de forma independente?

$
0
0
Criei um script o qual faz um GameObject se mover de forma senoidal. O movimento ocorre perfeitamente, mas quando mais de um GameObject contendo este script está em cena todos se movem como um espelho um do outro e não de forma relativa, deixando o jogo muito artificial. Tentei usar posicionamento local e mesmo assim não consegui resolver o problema. Eis o código: using System.Collections; using System.Collections.Generic; using UnityEngine; public class Enemy4 : Nave { public float horizontalSpeed; public int yFactor; private float count = 3; private Vector3 s; private bool setPos = false; private float x; private float y; // Use this for initialization void Start () { x = 0; y = 0; s = transform.position; side = Side.defside.ENEMY; } // Update is called once per frame void Update () { y += yFactor * Time.deltaTime; x = Mathf.Sin (Time.time) * horizontalSpeed; transform.position = s + new Vector3 (x,y, 0); } }

how to make enemy clone work independently?

$
0
0
so I am making a shooting game where the game spawns 5 enemies from a prefab. the problem is that when the enemy clone gets instantiated it works properly, but when the enemy clone detects the player, all of the enemy clones start shooting at the player. I want the enemy clone that detected the player to be the only one that will shoot at the player, and not all of the enemy clones. here is the code for my enemy object: using UnityEngine; using System.Collections; public class enemyMovement : MonoBehaviour { Rigidbody2D rb2d; public Transform target; private float nextFire; public float turnTime; public float speed; public float trackSpeed; int turn; public GameObject particles; int enemyHp = 100; private float explodeTime; // Use this for initialization void Start() { rb2d = GetComponent(); particles.gameObject.SetActive(false); target = GameObject.Find("fighter").transform; } float h, v, zed; public static bool check = true; // Update is called once per frame void Update() { float horizontal = Input.GetAxis("Horizontal"); float vertical = Input.GetAxis("Vertical"); if (check == true) { if (Time.time > nextFire) { nextFire = Time.time + turnTime; turn = Random.Range(0, 5); switch (turn) { case 1: h = horizontal - 1; v = vertical; rb2d.transform.eulerAngles = new Vector3(0, 0, 90); // Debug.Log("left"); break; case 2: h = horizontal + 1; v = vertical; rb2d.transform.eulerAngles = new Vector3(0, 0, 270); // Debug.Log("right"); break; case 3: h = horizontal; v = vertical + 1; rb2d.transform.eulerAngles = new Vector3(0, 0, 0); // Debug.Log("up"); break; case 4: h = horizontal; v = vertical - 1; rb2d.transform.eulerAngles = new Vector3(0, 0, 180); // Debug.Log("down"); break; } } Vector2 temp = new Vector2(h, v); rb2d.AddForce(temp * Time.deltaTime * speed); } else if (check == false) { Vector3 vectorToTarget = target.position - transform.position; float angle = (Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg) - 90; Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward); transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * speed); if ((target.position.x > rb2d.position.x + 7 && target.position.x rb2d.position.x - 15) || (target.position.y > rb2d.position.y + 5 && target.position.y rb2d.position.y - 15)) { transform.position += transform.up * Time.deltaTime * trackSpeed; }//if the enemy is within the range of the player, the enemy will follow the player. else if(target.position.x > rb2d.position.x + 15 || target.position.x rb2d.position.y + 15 || target.position.y explodeTime) { explodeTime = Time.time + .5f; particles.gameObject.SetActive(false); } } // Debug.Log(check); } void OnTriggerEnter2D(Collider2D other) { if(other.gameObject.CompareTag("player fighter")) { check = false; }// if the player touches the onTrigger for the enemy, the enemy will follow the player. } void OnCollisionEnter2D(Collision2D other) { if (other.gameObject.CompareTag("player bullet")) { Destroy(other.gameObject); enemyHp -= bulletMove.bulletDamage; } } }

How do I make and object's position and rotation independent again?

$
0
0
Just as the title says. How can I make the position of an object independent again after making it dependent earlier. This is what I have right now: if(ItemPickedUp == true){ // If you press the E key. if(Input.GetKeyDown("e")){ // The CarryableObject becomes a child of the Utility Props again. CarryableObject.transform.parent = UtilityProps.transform; // The drop position. CarryableObject.transform.position = new Vector3(Character.transform.position.x, Character.transform.position.y, Character.transform.position.z + OffsetDropPosition); // The rotation is no longer relative to the Character. CarryableObject.transform.rotation = Character.transform.rotation; // The gravity gets reenabled. CarryableObject.GetComponent().useGravity = true; // The child collider if-statements. if(Child1 != null){ Child1.GetComponent().enabled = true; } if(Child2 != null){ Child2.GetComponent().enabled = true; } if(Child3 != null){ Child3.GetComponent().enabled = true; } if(Child4 != null){ Child4.GetComponent().enabled = true; } } }

Touch Joystick behavior changes with resolution

$
0
0
I tried my game on mobile (tablet res: 1600*2560) and the joystick behaves different there. In free aspect and other default resolutions, it behaves normally. How can I make the behavior independent from screen resolution? Any help is highly valued.

Making an object resolution-independent

$
0
0

I'm working on a GUI that doesn't use Untiy's GUI system. Instead, it's composed of objects rendered by a second camera. How do I make these objects resolution independent? I gave it a shot and came up with this code:

static public Vector3 GUIPosition(float x, float y) {
    GameObject GUICamObject  = GameObject.FindWithTag("GUICam");
    Camera GUICamera = GUICamObject.GetComponent<Camera>();

    Vector3 pos = GUICamera.ViewportToWorldPoint(new Vector3((x)/100,-(y-100)/100,9));
    return pos;
}

static public void Element(GameObject button, float x, float y) {
    Vector3 buttonPosition = GUIPosition(x, y);
    button.transform.position = buttonPosition;
}

But it doesn't do the trick; on higher resolutions, object positions tend to distort anyways. I'm obviously going about it wrong; what's the "right" way to make an object resolution independent?

How Resolution Independence is Working

$
0
0
I know that Unity3D uses DirectX and OpenGL at backhand for rendering. I am curious about one thing that is Resolution Independence for 2D and 3D is provided by graphical API automatically ? Or Unity3D is providing that feature Because when i change my game resolution through changes automatically, i just want to know whether this is provided by graphical API ? Or engine is providing this component for graphical API for 2D and 3D

any good read about independent gui for android devices

$
0
0
where it is taken into account different screen densities? i am trying to make a gui that will be independent from resolutions and screen density. thanks!

Moving a single instance

$
0
0
I draw a line on which I want to add "handles", these "handles" can be moved and leave a line from their respective anchorpoint, to the new position of their respective "handle". So I made a prefab "handle" which gets instantiated on the line after a certain distance from the last "handle" was drawn. Each handle should be able to move independently, because I want to use the line length for other calculations. But when I have more than 1 instance, all overlap when I try to move a single "handle". Here's some code to elaborate: var rubberBand:GameObject; private var uniqueRubberBand:GameObject; private var rubberBandName:String; uniqueRubberBand = Instantiate(rubberBand, Vector3(linepos.x, linepos.y + 5, linepos.z), transform.rotation); rubberBandName = "RubberBand"; rubberBandName = rubberBandName.Insert(rubberBandName.Length, rubberBandNameArray.length.ToString()); Now for the line drawing, that is happening in a script attached to the prefab: function Start () { anchor = transform.position; drag = false; //Line1 line = new LineRenderer(); line = gameObject.AddComponent(LineRenderer); material = new Material(Shader.Find("Diffuse")); material.color = Color.red; line.material = material; line.SetVertexCount(0); line.SetWidth(startWidth, endWidth); //Line2 lineHelper2 = new GameObject(); lineHelper2.name = this.name + "lineHelper2"; line2 = new LineRenderer(); line2 = lineHelper2.AddComponent(LineRenderer); line2.SetVertexCount(0); line2.SetWidth(startWidth, endWidth); material2 = new Material(Shader.Find("Diffuse")); material2.color = Color.blue; line2.material = material2; //Line3 ... } function Update() {... line.SetVertexCount(2); line.SetPosition(0, anchor); line.SetPosition(1, anchor + unitVector * 25); line2.SetVertexCount(2); line2.SetPosition(0, anchor + unitVector * 25); line2.SetPosition(1, anchor + unitVector * 50); line3.SetVertexCount(2); line3.SetPosition(0, anchor + unitVector * 50); line3.SetPosition(1, transform.position); ...}

Framerate independent character movement

$
0
0
Hey guys and gals! For a networking project I need a framerate independent movement script (for obvious reasons). I saw this one on UnityCookie but it only moves on the global axis not the local axis. What do I need to change to make it move locally instead of globally? var speed : int = 5; var gravity = 5; private var cc:CharacterController; function Start (){ cc = GetComponent(CharacterController); } function Update (){ if(networkView.isMine){ cc.Move(Vector3(Input.GetAxis("Horizontal") * speed * Time.deltaTime, - gravity * Time.deltaTime, Input.GetAxis("Vertical") * speed * Time.deltaTime)); } else { enabled = false; } } Thanks in advance!

FixedUpdate and Time.deltaTime slowe

$
0
0
I am moving rigidbodies around with velocity. Is simply using function FixedUpdate(){ thisRigidbody.velocity.x = movement.x; } enough to make the movement framerate INDEPENDENT. versus function FixedUpdate(){ thisRigidbody.velocity.x = movement.x * Time.deltaTime; } Which seems to slowdown my game. Thanks in advance!

Keep SphereCollider radius the same, even though scale is changed

$
0
0
For a nice effect on a object I constantly change the scale of the transform, but this obviously changes the radius of the SphereCollider too. I need to find a way to keep the SphereCollider's radius the game, even though the scale of the transform changes. Any idea on how to do this? Help will be appreciated

Disable 3d engine OR Run at command line

$
0
0
I need to use the unity library, like Network.InitializeServer and networkView.RPC and was wondering if there was a way to do it without the bulk of the 3d engine. I realize that networkView would have to be faked, but is there a way to use the library outside of unity?

Framerate Independent Firing Rate (delta time?)

$
0
0
I am attempting to make a consistent firing rate. I have done a lot of research on figuring out consistent rates that are independent of framerate and most of the articles about it use deltaTime. However, most timers do not. I have a timer that works as long as the interval is not less that "0.2". Any number less than that has inconsistent results depending on the framerate. Specifically, when I play the project in the maximized window, versus a small window. Here is the code I am using. I know this has been beaten to death, so forgive me, please. I assume I must use some form of deltaTime multiplier somewhere. using UnityEngine; using System.Collections; public class Shooter : MonoBehaviour { // public GameObject bullet; // public Transform cam; public float interval; public float lastFire = -9999; public int shotsFired; void Update () { if(Time.time = lastFire + interval; } } I have toggled out the bullet references so that it can be easily tested. The reason I know it is inconsistent is because I implemented a test that lasts for a specific amount of time. I realize my test may be faulty and could be the actual problem. If so, please let me know. I really appreciate any and all help. Thanks, and God bless! Howey

Framerate independent physics (FixedTimestep, FixedDeltaTime)

$
0
0
How do I achieve 'correct' framerate-independent physics in Unity? As I understand it; physics logic should always be performed in the FixedUpdate() function (Application of forces, etc), but does this mean I should *scale* my forces by Time.fixedDeltaTime - similarly to the way we should scale by the [variable] deltaTime in the Update() function for game logic - or is this taken care of by the physics engine its-self? My reasoning for this is; if the number of required physics steps or the fixed update duration changes, then the forces applied will no longer be correct. However if the physics engine already takes this into account, then there is no reason to do the multiplication myself. I've read a few articles on the topic of physics in Unity, however there seems to be a lot of confusion and misinformation out there regarding how to properly achieve framerate-independence - perhaps this thread can clear some of that up? :)

Spinning a mesh independent of its forward movement?

$
0
0
This question is really about how do I create a prefab that allows for this. What I want is a billboard that will always face the camera while it is moving in space. I believe I do this with a gameobject that contains no mesh that has a child gameobject with the billboard mesh. A script I would add to child billboard gameobject would control its orientation to the camera. The parent gameobject would have a script attached that controls its movement. I am having trouble creating the basic prefab with the empty parent and the child with only the billboard in the Unity UI. When I try to drop in the child gameobject it merges rather creating the child. Can someone help me out with the steps to create this kind of a prefab?

Movement FPS Independent, how to?

$
0
0
Hello people, I've already asked this here a few times, but no one awsered, so let's try again. I've made a Character controller myself, and I tought i did things right when it comes to FPS independency, but when I tessted in other machines, it does'nt seem so. My code is pretty big. but the main lines are these: if(grounded == false) //Gravity for falling {finalmovement.y -= gravity * Time.deltaTime;} if(Input.GetButtonDown("Jump")) //Jumping {finalmovement.y = 7;} myTransform.position += (myTransform.rotation * finalmovement)*Time.deltaTime; //Overall movement I tought it is ok, if my character is not on the ground, the gravity is applied with Time.deltaTime, and if I jump, the Y of finalmovement goes to 7 and then starts to go down with the gravity to make my character move downward. But the problem is, when i have low fps, my character doesn't jump as high. why? It seems that the gravity is being applied more strongly ( I guess its because the Time between frames is bigger ), in other words, my movement is not FPS independent, even if I use Time.deltaTime. How can I solve this? Is my guess about the problem right? Thanks in advance.

List Problem :(

$
0
0
> using UnityEngine; > using System.Collections;> [System.Serializable] > public class pl> { public string name; public int> hp,maxhp; public int attack; }>> ________________________ > using UnityEngine; > using System.Collections;> using System.Collections.Generic;> public class player : MonoBehaviour {> public pl[] pl; public List pll;>> void Update(){>> } void OnGUI(){> if(GUILayout.Button("TIKLA")){> pll.Add(pl[0]);>> } } }> **> Sorry for very bad english :( When I add list same item.> When I change the name of the last item> Change is happening as I have all of the same item> I want to be independent of the items **

Jump not affected by changing velosity.y

$
0
0
Hello :) I've just done double jump in my game and in a some way it's affected by changing velosity.y by game object. It means the power of second jump depends on a time(?) and changing velosity.y(?) between double clicking "Space". I want to make it independent and the jump's power is need to be the same all the time. Where am I wrong? Thanks in advance! Tomas void Update () { if(Input.GetKeyDown(KeyCode.Space)) { if(grounded) { rigidbody2D.AddForce(new Vector2(0f, jumpingForce * moving)); grounded = false; jumpCounter = 1; } else if(!grounded && jumpCounter == 1) { jumpCounter++; rigidbody2D.AddForce(new Vector2(0f, jumpingForce * moving)); //Debug.LogError("And second jump"); } } }

Get real world's time which independent with device

$
0
0
I'm creating a game on Iphone/Android. In my game, user plants a tree then have to wait for that tree grow up. Each time user opens game, I check the time when user planted the tree and the current time, then decide the tree is grew up or not. The problem is, if user plants the tree, then change device's time, he/she can cheat to make the tree grow faster. How to avoid this? Is there a way to get the "real world current time" which user can not change?

Android Resolutions Independent

$
0
0
Hi! I started with unity some weeks ago. Im trying to make a simple side-shooter 2d game, i had set the camera with a script to 1280x720 so i can test it with my phone on landscape orientation. There are a lot of resolutions for android, how should i work around this for not getting sprites scaled or off screen? Thanks :)
Viewing all 102 articles
Browse latest View live




Latest Images