Question by
_Mike · Jan 16, 2016 at 04:38 PM ·
c#movement script
GTA 2 Movement Script
Hello I am trying to write game like GTA 2 in 2D(camera from the sky). I don't know how to do, that character looks in some direction and it moves in this direction, when user press up arrow. Sorry for my bad english. Please help.
using UnityEngine;
using System.Collections;
public class PlayerMovementRPG : MonoBehaviour {
public float speed;
public float turnspeed;
Rigidbody2D rbody;
private Transform playerTransform;
private Vector3 relative;
// Use this for initialization
void Start () {
rbody = GetComponent<Rigidbody2D> ();
playerTransform = GameObject.Find("Player").transform;
}
// Update is called once per frame
void FixedUpdate () {
//Player.transform.rotation = view;
//playerTransform.rotation = Quaternion.Euler(view.x, view.y, view.z);
//Vector3 playerPos = playerTransform.rotation;
Vector3 playerPos = playerTransform.eulerAngles;
float moveHorizontal = Input.GetAxisRaw ("Horizontal");
float moveVertical = Input.GetAxisRaw ("Vertical");
Vector2 movement_vector = new Vector2 (moveHorizontal, moveVertical);
rbody.MovePosition (rbody.position + movement_vector * speed * Time.deltaTime);
if (Input.GetKey (KeyCode.LeftArrow)) {
if (playerPos.z > 93 || playerPos.z < 87) {
transform.Rotate(0, 0 ,1 * turnspeed * Time.deltaTime);
}
}
if (Input.GetKey (KeyCode.RightArrow))
{
if (playerPos.z > 273 || playerPos.z < 267) {
transform.Rotate(0, 0 ,-1 * turnspeed * Time.deltaTime);
}
}
Debug.Log(playerPos.z);
}
}
Comment
Best Answer
Answer by hexagonius · Jan 16, 2016 at 04:51 PM
// Update is called once per frame
void FixedUpdate () {
Vector3 playerPos = playerTransform.eulerAngles;
if (Input.GetKey (KeyCode.LeftArrow)) {
if (playerPos.z > 93 || playerPos.z < 87) {
transform.Rotate(0, 0 ,1 * turnspeed * Time.deltaTime);
}
}
if (Input.GetKey (KeyCode.RightArrow))
{
if (playerPos.z > 273 || playerPos.z < 267) {
transform.Rotate(0, 0 ,-1 * turnspeed * Time.deltaTime);
}
}
if (Input.GetKey (KeyCode.UpArrow)) {
rbody.velocity = playertransform.up* speed * Time.deltaTime;
}
Debug.Log(playerPos.z);
}
Thank you, but now my character doesn't stop. It's moving all the time.
Your answer
Follow this Question
Related Questions
I can't seem to rotate my character depending on what side he is running to (2D sidescroller) 0 Answers
How can I get the player to face the direction it is going in a Unity 2D Game? 0 Answers
Movement input problem? 1 Answer
What is wrong with this script? 3 Answers
Object Won't move down on y axis until It moves up on the y axis or on any way of x axis 0 Answers