I need some help in trying to change the position of an gameobject and its speed. I know to translate the gameObject but I don't know how to increase the speed of how it translates. I'm using the unity game engine by the way.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class lightning : MonoBehaviour
{
//This is lightning
private GameObject name;
// Start is called before the first frame update
void Start()
{
name=GameObject.Find("car");
}
// Update is called once per frame
void Update()
{
if (Input.Keydone("r")
door.transform.Translate(-Vector3.right * Time.deltaTime);
}
}
One workaround you can use is a black filter(preferably Image) on UI layer that covers the whole screen. You can change filter's opacity accordingly to achieve the feeling of 'dimmed' screen.
It sounds like you are having trouble with speed and acceleration.
I added a velocity variable and an acceleration variable with the two you can make objects go faster and faster with time.
Since this is Physics I'd recommend you to use a rigidbody on your gameobject
and manipulate its speed or to apply a force (force = mass * acceleration)
so by adding a force it will give the object an acceleration.
//This is lightning
private GameObject name;
private float speedFactor = 20;
private float accelearationFactor = 5;
// Start is called before the first frame update
void Start()
{
name=GameObject.Find("car");
}
// Update is called once per frame
void Update()
{
if (Input.Keydone("r") {
door.transform.Translate(-Vector3.right * speedFactor * Time.deltaTime);
speedFactor += accelearationFactor * Time.deltaTime;
}
}
Related
I'm trying to make objects fly to the left while spinning themselves.
Here is the MoveLeft script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveLeft : MonoBehaviour
{
private float moveLeftSpeed = 10;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.Translate(Vector3.left * Time.deltaTime * moveLeftSpeed);
}
}
And there is the SpinObjects script:
using System.Collections.Generic;
using UnityEngine;
public class SpinObjectsX : MonoBehaviour
{
public float spinSpeed = 50;
// Update is called once per frame
void Update()
{
transform.Rotate(new Vector3(0, Time.deltaTime * spinSpeed, 0));
}
}
I expect the object's movement will look like this, it just moves to the left and spins itself.
But when I use both scripts, the object moves very weirdly, it is still spinning itself but instead of moving to the left, it spins around something...
Both Translate and Rotate per default work in local space of according object unless you explicitly pass in Space.World as additional last parameter.
So, after rotating your object around the Y-axis it's local left vector is now rotated as well and pointing somewhere else.
=> When you do the local space translation towards left of the object it doesn't actually move left in the world.
In order to move left in absolute world space you want to use
transform.Translate(Vector3.left * Time.deltaTime * moveLeftSpeed, Space.World);
I'm creating an air hockey game in Unity and I want the puck to sound like it's rolling across the canvas. I also want the sound to match the movement of the ball (get slower when the ball is slowing down, etc.) I searched for tutorials on this but I haven't found any good for C# and I've been struggling for days. I'm new to Unity and C#, so I'm not familiar with all functions and statements.
The simplest way to do it is as #akaBase said in the comments.
First, you'll want to get a good and loopable sound.
Second, you add an audio source component to the puck object and put the sound clip in.
Third, you need to make a script that looks at the current speed of the puck. If it's being moved by physics you can just request rigidbody.velocity every FixedUpdate or Update to change the audioSource.pitch.
Give it a try. Should be easy enough to figure out from here. You'll need to experiment a bit to see what sounds best, but if you still need help, just comment.
Here's a simple example script for changing pitch depending on the current speed of an object. Feel free to modify it - change the Update method to a FixedUpdate if you're gonna use physics, change how the speed is calculated/acquired etc.
using System.Collections;
using UnityEngine;
public class MovementBasedPitchAdjuster : MonoBehaviour
{
public AudioSource audioSource = null;
public float pitchAdjustingFrequency = 0.5f;
public float maxSpeed = 5f;
public float maxPitch = 1f;
private float speed = 0f;
private Vector3 lastRecordedPosition;
private float traveledDistance = 0f;
private void Start()
{
StartCoroutine(StartAdjustingPitchAndSpeed());
}
private void Update()
{
RecordTraveledDistance();
}
private IEnumerator StartAdjustingPitchAndSpeed()
{
while (true)
{
UpdateSpeed();
AdjustPitch();
yield return new WaitForSeconds(pitchAdjustingFrequency);
}
}
private void UpdateSpeed()
{
float newSpeed = Mathf.Sqrt(traveledDistance);
speed = newSpeed > maxSpeed ? maxSpeed : newSpeed;
traveledDistance = 0f;
}
private void AdjustPitch()
{
audioSource.pitch = speed / maxSpeed * maxPitch;
}
private void RecordTraveledDistance()
{
traveledDistance += Vector3.Distance(
lastRecordedPosition,
transform.position);
lastRecordedPosition = transform.position;
}
}
A test using an object that follow the cursor position.
https://streamable.com/fkw0k2
enter image description hereI am able to set Y axis of my player with a simple transform.position call, in a single step, all within onTriggerEnter method, but the motion has a single step and is therefore jerky. Now I am trying to make the motion smooth by putting the transform.position function in an Update method within the same class. However, it seems that the position values determined/updated by onTriggerEnter method are not accessible in the Update function. If I print the x and z values to console, they contain expected values from onTriggerEnter function, but appear to be 0 when I print to console from the update function.
Any ideas of what I am doing wrong?
I would never call myself a programmer, so assume the worst :-)
Thanks in advance for any help!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Step1SetElevation : MonoBehaviour
{
private float moveSpeed = 3f;
private float currX = 0.0f;
private float currZ = 0.0f;
private Vector3 currentPos;
private GameObject player;
private Collider other;
void OnTriggerEnter(Collider other)
{
player = GameObject.FindWithTag("Player");
currentPos = GameObject.Find("PlayerController").transform.position;
currX = currentPos.x;
currZ = currentPos.z;
}
void Update()
{
player = GameObject.FindWithTag("Player");
player.transform.position = new Vector3(currX, 3.4f, currZ) * Time.deltaTime * moveSpeed;
}
}
I believe your problem may be that you are using OnTriggerEnter() to set the position you want your player to move to, this will be called on the frame your player enters your trigger but then won't be called again until the player left and re-entered...
If you instead use OnTriggerStay() - something like
void Start(){ // this lookup can be expensive so lets only do it once
player = GameObject.FindWithTag("Player");
playerController = GameObject.Find("PlayerController");
}
void OnTriggerStay(Collider other){ // this is called once per frame that a collider remains in a trigger
if(other.gameObject.tag == "Player"){ // just in case anything else ever enters the collider
currentPos = playerController.transform.position;
currX = currentPos.x;
currZ = currentPos.z;
}
}
void FixedUpdate(){ // it's generally advised to move objects in fixed update
player.transform.position = new Vector3(currX, 3.4f, currZ) * Time.fixedDeltaTime * moveSpeed;
}
I have been trying to make a movement script for my player in a 2D game but without success. I do not know why it is not working.
The problem is that the player isn't moving. I have a RigidBody attached and gravity on. (Not sure if gravity makes such a difference but I just thought to mention it.)
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
public Rigidbody rb;
public float speed = 10;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void FixedUpdate () {
float mx = Input.GetAxisRaw("Horizontal");
float mz = Input.GetAxisRaw("Vertical");
Vector3 movement = new Vector3(mx, 0.0f, mz);
Debug.Log(movement);
rb.AddForce(movement * speed * Time.deltaTime);
}
}
You may wanna make sure you are adding enough force to actually cause the player to move. Try increasing the force variable incrementally until you see a change. Hope this helps!
I have an animated character , and I need move it forward
I add rigibody to my character , and mover class
when I disable animated controller it moves
my mover class
using UnityEngine;
using System.Collections;
public class Mover : MonoBehaviour {
public float speed;
// Use this for initialization
void Start () {
GetComponent<Rigidbody>().velocity = transform.forward * speed;
}
}
any suggestion how I can approach that
I find out the answer
just uncheck Apply Root Motion
Might want to put it in Update() so its not just called one time.
void Update () {
GetComponent<Rigidbody>().velocity = transform.forward * speed * Time.smoothDeltaTime;
}