beginners scripting problem using rotate and constant force
Unity beginner, First question, apologies for how wordy it is; i was folllowing a tutorial on youtube designing a pokemon-esque game, 2d tile built world with a character and script, playerStat.js and everything went well and works. I wanted to expand on this firstly by changing the way the character is controlled. What i want is a constant force that rotates exactly 90' on click or touch but only once per click or swipe, so i set about doing exactly that i added constant force to a rigidbody and added a rotate transform and it works... kind of.. right now the object keeps accelerating so i need to control the speed of the constant force or approach it a different way, and the rotation i have set at 180' as using 90' barely moved it at all but 360' in my script is still not enough to be 90' in the game, the camera is attached to the placeholder character and pressing 'a' or 'd' (my assigned keys) once will spin the character and camera about 70' and holding down those keys makes it spin longer whereas i need short 90' turn and holding down should do nothing. But the biggest mistake is that these scripts are seperate so playerStat.js has a way of walking across each tile as a single square and characterController script is just the left and right and i want to almost incorporate them both ive tried a few times to sort of merge them but i cannot make it work to make it worse one script (the tutorials script) is javascript and my script is in C# Maybe some body can help me i bet its not as hard as im making it out any advice appreciated
heres the js script player stat, it knows where the tiles are in my game and will walk 1 space at a time. so it doesnt pause to turn
#pragma strict
var startPoint : Vector3;
var endPoint : Vector3;
var speed : float;
private var increment:float;
var isMoving : boolean;
function Start () {
startPoint = transform.position;
endPoint = transform.position;
}
function Update () {
if(increment <=1 && isMoving == true){
increment += speed/10;
Debug.Log("Moving");
}else{
isMoving = false;
Debug.Log("Stopped");
}
if(isMoving)
transform.position = Vector3.Lerp(startPoint, endPoint,increment);
if(Input.GetKey("w")&& isMoving == false){
increment = 0;
isMoving = true;
startPoint = transform.position;
endPoint = new Vector3(transform.position.x,transform.position.y,transform.position.z+1);
}
else if(Input.GetKey("s")&& isMoving == false){
increment = 0;
isMoving = true;
startPoint = transform.position;
endPoint = new Vector3(transform.position.x,transform.position.y,transform.position.z-1);
}
else if(Input.GetKey("a")&& isMoving == false){
increment = 0;
isMoving = true;
startPoint = transform.position;
endPoint = new Vector3(transform.position.x-1,transform.position.y,transform.position.z);
}
else if(Input.GetKey("d")&& isMoving == false){
increment = 0;
isMoving = true;
startPoint = transform.position;
endPoint = new Vector3(transform.position.x+1,transform.position.y,transform.position.z);
}
}
and this is the cs script just taking a left and right
using UnityEngine;
using System.Collections;
public class characterController : MonoBehaviour {
public int rotateSpeed = 180;
// Use this for initialization
void Start () {
Rigidbody rb = GetComponent<Rigidbody>();
rb.AddForce(0, 0, 0);
}
// Update is called once per frame
void Update () {
transform.Translate(Input.GetAxis("Horizontal") * Time.deltaTime, 0f, Input.GetAxis("Vertical") *
Time.deltaTime);
transform.Rotate(0, Input.GetAxis("Rotate") * rotateSpeed * Time.deltaTime, 0);
}
}
Your answer
Follow this Question
Related Questions
Restrict second touch 0 Answers
Addforce to Z axis rotation via touch input/ mobile 0 Answers
Issues With Touch Rotation code 0 Answers
Smooth rotation with CrossPlatformInput 0 Answers
Rotate Player w/Touch Input Controls 0 Answers