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.
Related
I am making my object follow the rotation of another object. I want my object to rotate with very little difference, that is, from its current rotation it should not rotate completely with the other object.
There should be a difference u pto its rotation such that it should rotate 10% of what the main object rotates. How do I do this?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowRotationWithLimit : MonoBehaviour {
public GameObject objectToFollow;
// Start is called before the first frame update
void Start () {
}
// Update is called once per frame
void Update () {
this.transform.eulerAngles = new Vector3 (this.transform.eulerAngles.x, objectToFollow.transform.eulerAngles.y, this.transform.eulerAngles.z);
}
}
I am not able to get that logic of creating that difference.
I would rather call it FollowRotationWithFactor then and simply use a multiplier
public class FollowRotationWithFactor : MonoBehaviour
{
public GameObject objectToFollow;
public float factor = 0.1f;
// Update is called once per frame
void Update ()
{
var eulerAngles = transform.eulerAngels;
eulerAngles.y = objectToFollow.transform.eulerAngles.y * factor;
transform.eulerAngles = eulerAngles;
}
}
Below is the code I'm using. Basically, it is a ball with the sprite of a tiny
spaceship that rotates around a ring. It has a trail renderer attached and
when I change the direction the trail comes out the top in the wrong direction.
I need to know how to flip the ship to match the trail.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BallRotation : MonoBehaviour
{
[SerializeField] private float _speed;
private void FixedUpdate()
{
transform.Rotate(0, 0, _speed * Time.deltaTime);
}
public void ChangeDirection()
{
_speed = -_speed;
}
}
Hard to pinpoint ur error without knowing further details.
Is your "ball" a 3D Sphere or a 2D Circle?
What component is used to display your spaceship image?
if its the SpriteRenderer, you can get a reference to that and flip the sprite
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BallRotation : MonoBehaviour
{
[SerializeField] private float _speed;
SpriteRenderer m_SpriteRenderer;
void Start()
{
//Fetch the SpriteRenderer from the GameObject
m_SpriteRenderer = GetComponent<SpriteRenderer>();
}
private void FixedUpdate()
{
transform.Rotate(0, 0, _speed * Time.deltaTime);
}
public void ChangeDirection()
{
_speed = -_speed;
//flip sprite along the y axis
m_SpriteRenderer.flipY = !m_SpriteRenderer.flipY
}
}
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");
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;
I am a bit confused as to why this is, so basically Im trying to get it to where the camera follows the player without moving left and right with him.
using System.Collections;
using UnityEngine;
public class CameraMotor : MonoBehaviour {
private Transform lookAt;
private Vector3 startOffset;
private Vector3 moveVector;
void Start () {
GameObject.FindGameObjectWithTag ("Player").transform;
}
// Update is called once per frame
void Update () {
moveVector = lookAt.position + startOffset;
//X
moveVector.x = 0; //center of track
//Y[image][1]
moveVector.y = Mathf.Clamp(moveVector.y,3,5);// for ramps/stairs
transform.position = moveVector;
}
}
I think you want GameObject.FindWithTag
https://docs.unity3d.com/ScriptReference/GameObject.FindWithTag.html
Even so,
GameObject.FindWithTag ("Player").transform;
does nothing. (Nothing usefull atleast)
lookAt is never assigned, so im guessing what you want to do is
lookAt = GameObject.FindWithTag ("Player").transform;
If you are looking for an object in your scene, then use this simple script;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AnswerScript : MonoBehaviour {
private Transform lookAt;
// Use this for initialization
void Start () {
lookAt.Find("The Object That You are Looking For");
}
// Update is called once per frame
void Update () {
}
}