Find the value of some floats in my animator - c#

I need to find the value of the variable "vertical" and see if it is equal to zero.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AnimationStateController1 : MonoBehaviour
{
public Animator anim;
// Update is called once per frame
void Update()
{
anim.SetFloat("vertical", Input.GetAxis("Vertical"));
anim.SetFloat("horizontal", Input.GetAxis("Horizontal"));
}
}

There is a GetFloat method that is the inverse of SetFloat.
var vertical = anim.GetFloat("vertical");

Related

Checking the amount of enimies in unity c# is not working for me

I am trying to make it so that there is a counter for the amount of enimies in the current level, and I found some code that should check the amount of enimies left. After you kill them, a destroy(gameobject) command is in place. For some reason, whenever I try to use this, the text does not update and stays on "new text".
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class text : MonoBehaviour
{
public TextMeshProUGUI txt;
private GameObject[] getCount;
public float count = 0f;
// Start is called before the first frame update
public void Start()
{
}
// Update is called once per frame
void Update()
{
getCount = GameObject.FindGameObjectsWithTag("enemy");
count = getCount.Length;
txt.SetText(count.ToString());
}
}

Unity OnControllerColliderHit detecting not detecting correctly

I am trying to teleport the character when they touch a cube, but for some reason, it prints the message that it the collision is being detected, but the character's position is not changing. It works for another scenario that is almost exactly the same, but even if I put this in another script, it does not teleport the character. Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class othercollision : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnControllerColliderHit(ControllerColliderHit hit)
{
if (hit.gameObject.name == "teleportcube")
{
gameObject.transform.position = new Vector3(-3184.53f, 20.35f, -171.585f);
Debug.Log("Collision detected");
}
}
}
Here is the script that has something similar working:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading;
public class detectcollision : MonoBehaviour
{
public GameObject tplocation1;
public GameObject player;
public GameObject entranceloc;
void OnControllerColliderHit(ControllerColliderHit hit)
{
//This if statement does Debug.Log the message and it also changes the player's position.
if (hit.gameObject.name == "trigger")
{
Debug.Log("triggered");
gameObject.transform.position = tplocation1.transform.position;
Thread.Sleep(7);
Application.Quit();
}
//This if statement does not change the player's position, but Debug.Logs the teleporting message.
if (hit.gameObject.name == "entrance")
{
gameObject.transform.position = new Vector3(-3184.53f, 20.35f, -171.585f);
Debug.Log("teleporting...");
}
}
}
Try to make the Vector3 beforehand as a variable or a temporary variable and then assign it to the position. If you did then maybe the values that you have entered are out of boundary somewhy, but that's unlikely, try changing them.

How Do I Change the Text of a TextMeshPro Object in Unity?

I'm trying to make a TextMeshPro object update to show the value of a sensitivity slider in my game. However, it doesn't update!
I've tried looking it up but almost every tutorial I've followed hasn't helped.
Here is the 'PlayerScript' script, which gets the value on the slider (this functions):
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class PlayerScript : MonoBehaviour
{
// Update is called once per frame
public float moveSpeed = 600f;
public static float publicSensitivity;
float movement = 0f;
void Update()
{
movement = Input.GetAxisRaw("Horizontal");
}
public void SetSensitivity(float sensitivity)
{
Debug.Log(sensitivity);
moveSpeed = sensitivity * 12;
publicSensitivity = sensitivity;
}
void FixedUpdate()
{
transform.RotateAround(Vector3.zero, Vector3.forward, movement * Time.fixedDeltaTime * -moveSpeed);
}
}
and here is the 'ChangePercentage' script, which is meant to change the TextMeshPro text object to reflect the value on the slider (not changing. By default, the object reads "foo". When I open the options menu, it changed to read 0%, but only once. This means that it must update but not continuously):
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class ChangePercentage : MonoBehaviour
{
public TextMeshProUGUI self;
void Update()
{
self.text = (PlayerScript.publicSensitivity).ToString() + "%";
}
}
Thanks in advance!
How/where is the SetSensitivity method called?
I would recommend to use the slider object and then set the sensitivity to slider.value .
Example:
unsing UnityEngine.UI;
public slider sensitivity_slider;
public static float sensitivity;
public void Update(){
sensitivity = sensitivity_slider.value;
}
You can alsow make a script that displays the slidervalue directly.
Hope i could help, sorry for my bad english.

Trying to grab a variable from one script and apply to another

I'm trying to use a UI Slider to change the movement speed for my player character using the GetComponent feature. I have everything working except apply the number(float) that I create from the movement of the slider to the variable that controls how fast the player can move.
I've used Debug.Log(); to determine that the variable I'm trying to grab from one script does not equal the other. It almost seems that they're being stored as two separate variables.
The varspeed variable keeps track of the number when I move the slider.
In script BallScript:
GameObject.Find("Canvas").GetComponent<PointBuyScript>().varspeed = speedvar1;
Debug.Log(speedvar1);
In script PointBuyScript:
public void Start()
{
mySpeed.onValueChanged.AddListener(delegate { ValueChangeCheck(); });
}
public void LateUpdate()
{
varspeed = mySpeed.value;
Debug.Log(varspeed);
}
When I move the slider the number in the console from PointBuyScript scales with the Slider. However, the one from BallScript forever remains the same.
BallScript Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BallScript : MonoBehaviour
{
// Start is called before the first frame update
public float speed;
private Rigidbody rb;
public float speedvar1;
public float SpeedMain;
void Start()
{
rb = GetComponent<Rigidbody>();
speedvar1 = GameObject.Find("Canvas").GetComponent<PointBuyScript>().mySpeed.value;
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.00f, moveVertical);
speed = speedvar1; // this is where I try and update the speed variable to the slider number
rb.AddForce(movement * speed);
}
void LateUpdate()
{
Debug.Log(speedvar1);
Debug.Log(speed);
// Debug.Log(SpeedMain);
}
}
PointBuyScript Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PointBuyScript : MonoBehaviour
{
public Slider mySpeed;
public float varspeed;
public float mainSpeed;
public void Start()
{
// GameObject speed1 = GameObject.Find("Ball");
// BallScript hellome = speed1.GetComponent<BallScript>();
// varspeed = GameObject.Find("Ball").GetComponent<BallScript>().speed;
//Adds a listener to the main slider and invokes a method when the value changes.
mySpeed.onValueChanged.AddListener(delegate { ValueChangeCheck(); });
}
public void Update()
{
// Debug.Log(mySpeed.value);
//mainSpeed = mySpeed.value;
}
public void LateUpdate()
{
varspeed = mySpeed.value;
Debug.Log(varspeed);
}
// Invoked when the value of the slider changes.
public void ValueChangeCheck()
{
}
}
This code:
GameObject.Find("Canvas").GetComponent<PointBuyScript>().varspeed = speedvar1;
Says "take the value in speedvar1 and assign it to PointBuyScript#varspeed." That is, the value of PointBuyScript#varspeed is changed (and speedvar1 remains unchanged).
You probably want:
speedvar1 = GameObject.Find("Canvas").GetComponent<PointBuyScript>().varspeed;

Movetowards , Addforce , translate none is working why?

I just want to move a bullet (but it doesn't work) and test on cube (but the code too did not move the cube).
The commented codes also did not move the game object.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class movetest : MonoBehaviour
{
//public GameObject cube;
// Use this for initialization
public GameObject testmovecube;
public Rigidbody rb;
void Start () {
//testmovecube = this.GetComponent<GameObject>();
//rb = testmovecube.GetComponent<Rigidbody>();
move();
//
}
// Update is called once per frame
void Update () {
}
private void move()
{
testmovecube.transform.position =
Vector3.MoveTowards(transform.position, new Vector3(226, 1, 226) , 2);
//transform.Translate(Vector3.forward);
//rb.AddForce(Vector3.forward);
}
}
Please any help is appreciated.

Categories

Resources