Moving the character LEFT and RIGHT relative to its rotation
Hi, so as the title says I want to add left and right movement to my character relative to its rotation. I've done this to forwards and backwards, but left and right isn't as simple.
Code :
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour {
public float speed;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKey (KeyCode.W)) {
transform.position += transform.forward * (float)Time.deltaTime * speed;
}
if (Input.GetKey (KeyCode.S)) {
transform.position -= transform.forward * (float)Time.deltaTime * speed;
}
}
}
Thats its, quite simple. So once again, I want to add right and left movement relative to the rotation of the player.
Also, this is for a top down shooter! this may or may not clear some things up.
Hi @Peace , I got this answer from Unity answers itself. You may try this..
//$$anonymous$$ovement
//$$anonymous$$ove forwards and backwards
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.W))
moveDirection.x -= 1;
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.S))
moveDirection.x += 1;
//$$anonymous$$ove up and down
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.E))
moveDirection.y += .5f;
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.Q))
moveDirection.y -= .5f;
//Turn left and right
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.D))
gameObject.transform.Rotate(0, 90 * Time.deltaTime, 0);
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.A))
gameObject.transform.Rotate(0, -90 * Time.deltaTime, 0);
//Update position
transform.position += moveDirection.x * transform.right;
transform.position += moveDirection.y * transform.up;
Hope this will help you..
Answer by miszelfox · Apr 02, 2016 at 06:00 PM
Instead of changing transform.position you can try using transform.translate.
if (Input.GetKey (KeyCode.A)) {
transform.Translate(Vector3.left * Time.deltaTime * speed, Space.Self); //LEFT
}
if (Input.GetKey (KeyCode.D)) {
transform.Translate(Vector3.right * Time.deltaTime * speed, Space.Self); //RIGHT
}
Space.Self makes it relative to local rotation. Make the same with forward and backward by using Vector3.Forward and Vector3.Back
Hi thanks for sharing this. It has saved me hours of research and trial and error! BTW, I'm new to game making :)
Answer by Kureiji · Apr 02, 2016 at 11:37 AM
Hi !
I hope this answer your question :
public float speed = 1f;
void FixedUpdate () {
// 1. Get the Input information
// You can custom the axis key and sensibility at
// Edit > Project Settings > Input
float h = Input.GetAxis ("Horizontal");
float v = Input.GetAxis ("Vertical");
// 2. Create the move vector
// /!\ Input Horizontal axis positive button
// need to be the right move button (right arrow, D, ...)
Vector3 moveVector = (transform.forward * v) + (transform.right * h);
moveVector *= speed * Time.deltaTime;
// 3. Apply the mouvement to the local position
transform.localPosition += moveVector;
}
With this input you'll have a smoothed mouvement, if you want it sharp change the axis parameters in Edit > Project Settings > Input
Bye !