- Home /
How to add Speed
Hello Guys, i just want to ask if you have an idea to give a speed of rotation and movement of and object. This is my script I hope you can help me. using System.Collections; using System.Collections.Generic; using UnityEngine;
public class NewMov : MonoBehaviour { private void Update() {
if(Input.GetKey(KeyCode.R)){ transform.Rotate(new Vector3(0f, 180f, 0f));}
else if (Input.GetKeyDown(KeyCode.UpArrow) && transform.position.x > 0 )
{
targetPos = new Vector3(transform.position.x - move , transform.position.y, transform.position.z );
transform.position = targetPos;
}else if(Input.GetKeyDown(KeyCode.DownArrow) && transform.position.x < 3)
{
targetPos = new Vector3(transform.position.x + move, transform.position.y, transform.position.z );
transform.position = targetPos;
}
}
}
Comment
This is the script
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class NewMov : MonoBehaviour { // Start is called before the first frame update private Vector3 targetPos; public float move;
public int score;
public int health = 3;
private void Update()
{
if(Input.GetKey(KeyCode.R)){
transform.Rotate(new Vector3(0f, 180f, 0f));}
else if (Input.GetKeyDown(KeyCode.UpArrow) && transform.position.x > 0 )
{
targetPos = new Vector3(transform.position.x - move , transform.position.y, transform.position.z );
transform.position = targetPos;
}else if(Input.GetKeyDown(KeyCode.DownArrow) && transform.position.x < 3)
{
targetPos = new Vector3(transform.position.x + move, transform.position.y, transform.position.z );
transform.position = targetPos;
}
}
}
Answer by Baymobile · May 11, 2021 at 09:36 AM
Hi may be better
public class NewMov : MonoBehaviour
{
[SerializeField] int score;
[SerializeField] int health = 3;
[SerializeField] private float _moveSpeed = 1f;
// MOVE
private void Update()
{
if(Input.GetKey(KeyCode.R))
{
transform.Rotate(new Vector3(0f, 180f, 0f));}
}
else if (Input.GetKeyDown(KeyCode.UpArrow) && transform.position.x > 0 )
{
transform.Translate(Vector3.forward * _moveSpeed * Time.deltaTime, Space.World);
}
else if(Input.GetKeyDown(KeyCode.DownArrow) && transform.position.x < 3)
{
transform.Translate(Vector3.back * _moveSpeed * Time.deltaTime, Space.World);
}
}
}