New answers tagged

0

Check if button is held down by user. create a bool value and turn it true if the button is held down and false if not. then move the player is bool value is true and stop if false.


0

Is your player's rotation always controlled by animation, and is there a rigidbody attached to the child object? If so, this would explain the discrepancy you are seeing. The values you see in the inspector will be after the animations are applied, while the values you get during Update will be from before. Do you get different values in your replay ...


0

You can solve this by using the ability of the SceneManager to load scenes additively. Create one base-scene with everything which is supposed to be present in every level (like the player-character, for example). Nothing in this scene requires DontDestroyOnLoad. Create every level as a separate scene. Those scenes should not contain anything from the ...


0

I am not aware of an easy way of doing this. The way I'd do this is the following: Determine XY position of click on texture (x = 0..1, y = 0..1) How to do this depends on where you use it, but will likely require raycasting. Go to the camera that sends the image to the texture. Assuming the camera uses perspective projection: Calculate the 3D ...


0

You could add a child to the barrel and add a script to the child that will set one of its rotation axis to the starting value which means one of its rotation axis eg. forward will always stay the same and it could act as an indicator of the forward direction of the barrel since it will be moving and rotating on other two axis the same as the barrel. Your ...


0

Firstly add a tag to the platform object eg. "platform", then in the script where you are rotating the object make sure it checks if certain Boolean eg. ROT is true, then in the raycast from mouse script check the tag of the object like this: if(hit.collider.tag == "platform") If it is then set the Boolean ROT to false which will prevent the line rotating ...


0

Simply add this line in your Start() function: DontDestroyOnLoad(gameObject); this shall enable your object to continue on to the next scene, however I'm not sure if this will work for the audio to play continuously, if it doesn't then what you could do is before loading a new scene get the playback time of the audio currently, and then in the new scene just ...


2

Are you trying to use CompareTag on the gameObject that your RaycastHit is returning? If so it looks like you would need to do "hit.collider.gameObject.CompareTag". Or if that doesn't work, you would need to move that condition into a nested if statement. So: if (Input.GetMouseButton(0) && Physics.Raycast(ray, out hit, 10f) && ...


1

Rigidbody Contraints is a class to use for the rigidbody. In your case, instead of using hit.transform. = RigidbodyConstraints.FreezeRotation;, I would replace that with hit.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotation;I see that you have tried to set a Rigidbody class on a Transform. Rigidbody constraints take a ...


3

When you write transform without anything in front of it, it refers to this.transform, ie. the transform attached to the GameObject this script instance is running on. It sounds like you instead want to modify the transform of the object hit by the raycast. To do this, we need to capture a reference to it... Ray ray = new Ray(transform.position, ...


0

I would do both. I would make separate arms, legs, body, and head classes and keep track of them all within a player class. This way, in your Game1 class and other classes, the player is treated as ine object, but within the class, the body parts are treated separately. Hope this helps!


1

I would advise using neither for this particular case. InvokeRepeating takes a method name, which means it's using reflection at runtime to look up the method to call. That's work we don't need to do, since we can already point the compiler at the exact method we want. Update executes every frame, but for spawning we typically want to leave some time (or ...


0

When you load a scene from a script you cant execute code after that.You need to deserialize your player data and then set the position in a Awake() or Start() method. They get called once when the scene is loaded.


1

I hope this code I wrote helps you. If you have any more questions please ask. using UnityEngine; using UnityEngine.UI; public class NewBehaviourScript : MonoBehaviour { private AudioSource src; public Slider slider;// This is so we can drag the slider to the inspector view ot this script // Use this for initialization void Start () { ...


3

ally = (GameObject) ...is a syntax error. You need to comment this line just like you did the one below it. The problem is on that line, but because it's so malformed (no semicolon at the end), the compiler doesn't recognise it and assumes the problem is on the next line, transform.position = new Vector3(transform.position.x, transform.position.y - -5, ...


0

That jitter you see comes from the way floating point numbers work. A float has only a fixed number of significant digits. If they are storing a large number, the decimals suffer from loss of precision. Let me put an example using decimal numbers. Suppose floats can hold say, 5 significant digits only. Also, let's say the ship is positioned at 10.000, If ...


2

It has to do with the way the coordinates systems are set. In particular, what it means to increment the Y coordinate for the mouse? an on the screen?. I'm not sure which convention Unity follows, but to put an example. The mouse coordinates might be (0,0) at the top-left corner and Y would increase when moving the mouse downward. On the other hand, the ...


0

Since your bounds (-4,-4) -> (4, 4) are world space coordinates, your error is that you're clamping the input (screen space) instead of the output of ScreenToWorldPoint. Input.mousePosition will give you pixel coordinates between (0,0) and the max resolution of your screen (Screen.width, Screen.height). Try clamping the result after the call to ...


2

The yield return new WaitForSeconds(time) construct only works in Coroutines. A coroutine must be started with the StartCoroutine(name) method and must itself be a method which returns an IEnumerator. So first you need to write your coroutine as a new method: IEnumerator FadeToNextLevel() { float fadetime = ...


0

I made some changes and simplified the code for you. You will need to set the collider on the Teleporter as a trigger and then drag in the object that you want your destination to be as well as your player character. using UnityEngine; using System.Collections; public class playerTeleport : MonoBehaviour { bool isInRange = false; public Transform ...


0

Under canvas create empty GameObject, add Image component, assign sprite to Image.sprite


1

Animator.Play() works with the state in the Animator not the actual animation. I'm guessing you are not returning from the Flip-Forward state. Make sure to create a transition from to that state back to the default state and that the transition Has Exit Time property be ticked. This is a simple example of how the state machine should look. However I ...


0

Create a Ui Button at runtime, Working example without prefabs. public static UnityEngine.UI.Button UiTexturedButton(Sprite sprite, Vector2 size, GameObject canvas) { GameObject go = new GameObject("Textured button (" + sprite.name + ")"); Image image = go.AddComponent<Image>(); image.sprite = sprite; ...


1

use a mesh collider instead ! i just had the same problem. but then i just used a mesh collider instead of a sphere collider and the problem almost solved itself just be sure to update the mesh collider shared mesh as well: var meshInstance = Instantiate(meshes[type]); gameObject.GetComponent<MeshFilter> ().mesh = meshInstance; ...


2

This can be done with a very small modification to the script in the question Philipp linked: public class BlendFollower : MonoBehaviour { public Transform leader; public float followSharpness = 0.1f; Vector3 _followOffset; void Start() { // Cache the initial offset at time of load/spawn: _followOffset = ...


2

You can actually do one better. If it's the Test2 component you care about, then make that the type of the inspector variable you expose: private int bye = 1; public Test2 playerTestScript; void Start(){ playerTestScript.hello *= bye; Debug.Log(playerTestScript.hello); } This way you have absolutely no searching to do at runtime. Everything is ...


0

Because you need it to be picky (like a security code <- hint hint) use a more low-tech method. Divide your rune drawing area into a grid of NxN (5x5 works well, 3x3 for a very small rune selection). Consider the strike to hit the area only when it is within a certain distance from the center of the grid cell. Record the cell hits in order as a string ...


2

Find the normal between the two then scale that normal by the distance wanted. float distanceWanted = 3.0f; Vector3 diff = transform.position - player.position; diff.y = 0; // ignore Y transform.position = player.position + diff.normalized * distanceWanted;


0

Try this: calculate the angle between the player and the object on the X and Z axis find the offset on the X and Z axis by using cosine and sine of the previous extabilished angle and multiplying them by the distance you want to keep move the object to that offset


2

You can do this with two cameras with different culling masks. When you have multiple cameras in your scene, each camera will be rendered separately. The culling mask decides what will be rendered by the camera. Then the output of all the cameras get drawn on top of each other in order of their "Depth" value. Click on "Layers -> Edit Layers" and create a ...


-3

Try drawing the sprite before anything else.


0

Apparently there were a few more objects with those tags which were colliding with the object and that's why this "bug" appeared. Deleted those objects and fixed it.


0

Quaternions are different than Euler angles; Euler angles has 3 components (x, y, z) while Quaternions has 4 (x, y, z, w). If you want your result in Euler angles, use following code piece: Quaternion LeftTurnLimit = Quaternion.identity; float test = 0.1f; LeftTurnLimit = Quaternion.Euler(test, 0, 0); var LeftTurnLimitEuler = LeftTurnLimit.eulerAngles; ...


4

Rounding. If you attach the debugger and look at the values of your quaternion: You'll see that the values are just too small to be displayed with one tenth precision, as the debug value is displayed. You can confirm this by looking at: Debug.Log(LeftTurnLimit.eulerAngles); This will output the initial value you put in : 0.1, 0, 0 To address the ...


3

Let's take a closer look at what's actually happening here step by step. var sprite You create a new variable sprite which doesn't point to anything yet. sprite = image.sprite; And in the same line you point the variable to the same object as a different variable image.sprite is pointing to. sprite = obstacles[obstacleIndex].GetIcon(); Now you ...


2

sprite is changed! But, that's not what you want. You want to change image.sprite. They are both different references to some Sprite. You're creating a second reference and then changing the second reference, rather than the reference image has. So just change image.sprite, not sprite.


1

After a bit of thought but no testing, it depends on your rockets. If the rockets are going to hit only the player then check on the script attached to the player and do everything there including destroying the rocket. If your rockets are going to hit more than the player then it's best to let the rockets do the collision checks on the rocket and have it ...


1

After some consideration I'm completely changing my previous answer. It would be more effective to implement a OnCollisionEnter method on the player that will do logic when a rocket hits him. That way less collision will have to be calculated and the physics engine will only have to keep track of the position of the rockets colliders and the rockets ...


0

Honestly, I think trying to do this with the CPU is just way more work than it's worth. This is the kind of texture operation GPUs were made to simplify & accelerate for us. This animation was created using this shader code: Shader "Unlit/Rorschach" { Properties { _MainTex ("Texture", 2D) = "white" {} _BlackLevel("Black ...


1

Unity does not store this information, so there is no property or field that holds the old position of objects. Once a frame passes, the old positions of objects are gone. You can get around this by storing the last few positions yourself and interpolating between them. Here's some code: using UnityEngine; public class Tracker : MonoBehaviour { public ...


0

Note: I'm by no means the lead authority on this, but I believe you've combined two different methods of saving the data. Player Prefs don't require the binary formatter stuff Binary Serialization does not use player prefs I'd say player prefs are easier to use (although not as great for saving lots of data) To save the PlayerPrefs it's just ...


1

I had your same problem. In my case It got fixed by closing unity and reconnecting the PS3 controller before starting Unity again. Maybe it helps


0

Yeah, a simple for loop will suffice for this. for (int i = 0; i > Hearts; i++) { int col = i % 9; int row = Mathf.Floor(i / 9f); GUI.DrawTexture(new Rect(10 + (74*col), 10 + (74*row), 64, 64), heartTexture, ScaleMode.ScaleToFit); } Also, note the names / values may be changed, if your hearts are smaller, or you'd like less space in between ...


3

What you describe, in terms of following a position's exact path with a time delay, might be both more complicated and less satisfying than what you want. Here's a mock-up of two possibilities: The object labeled "Path" follows the exact path of the leader object, a constant amount of time behind it. This means: When the leader starts moving, there is ...


0

You can define an integer value to use as counter, meaning how long the user has kept his finger on the screen to track a long press: int timer = 0; Then, you're going to detect just an event - the mouse pressed event - and increment the timer at your will: void Update() { // Increment the timer if (Input.GetMouseButton (0)) { timer ...


0

I think you should not update database whenever something changed by player. You should update database in a sufficient frequency. Before updating database you should hold frequently changed data in the memory; a database updater thread can update/store database for instance at 1 hz.


0

When this is for an online game, you should definitely store that information online on your server. When you store the character state on the client, your players will edit it to cheat. The MM in MMORPG stands for "Massive Multiplayer", as in "Too many players to store in regular flatfiles". You will definitely need a database which is used by your server ...


0

Assuming you don't care about changes in direction you can multiply the velocity vector of the object by a negative amount, then add that negative vector to the position of the object you're trailing. I think it would be something like this: var positionBehind = rigidbody.velocity * -1.0f; var trailPosition = rigidbody.position + positionBehind;


0

The simple solution is to add a bool property to the door indicating if its open or not. Then you simply check: if(ohter.gameObject.CompareTag("Player") && Door.isClosed) { //Do logic here } You can even set the isClosed to false when the open animation has finished using a Animation Event.


0

u need Input.gyro.userAcceleration Edit: for some Android devices I had to manually call Input.gyro.enabled = true; not sure if it's the same for Samsung-GearVR



Top 50 recent answers are included