I have my script written but when I move the character two things happen that I dont like. First my character doesnt move in the direction its facing or the direction of the key I pressed and second when I click the right or left keys my character only rotates I want it to move left and right as well.
Thanks
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Character1AnimControl : MonoBehaviour {
static Animator anim;
public float speed = 2.0f;
public float rotationspeed = 75.0f;
// Use this for initialization
void Start () {
anim = GetComponent();
}
// Update is called once per frame
void Update () {
One thing that stands out, is that youāve named 2 variables ātranslationā. Iām pretty sure thatās not even allowed.
Do you have errors from this script?
Okay, well if youāre moving and rotating at the same time⦠it might be āoddā to find āforwardā , as it will be always changing.
You never applied the sideways movement to the transform.Translate portion. Thatās why it never moved to the side.
However, Iām not sure if just doing that will fix it all for you because of the turning part.
Does that make sense?
well, as I said I donāt think it will fully fix it⦠but
see how you have transform.Translate(0,0,-translation); ?
change that to: transform.Translate(rotation, 0, -translation);
Before, you werenāt using the horizontal value to move (only rotate). Thatās why it was like that.
Okay, so thereās an issue with turning (rotating) and moving. If I go left, and Iām also rotating left⦠do I just keep rotating & going left forever?
Plus, if I rotate and I want my ācurrent leftā to alter with the rotation, then Iāll just be doing a circle.
So, do you want the character to move sideways while keeping its āfacingā direction, or just turn to the side like 90 degrees and move? Thatās how I am thinking about it, anyways.
This would move you in all directions (including diagonal) but doesnāt do any rotation:
Ok Im trying to get it so when I press W to go forward my character turns in the forward direction, starts walking then if I like turn left and hit A the character turns -90 degress left and continues walking.
Right now the character moves forward and back when I press A and D and moves left and right when I press W and S all the while facing the same direction which I dont like.
Hopefully thats enough info and heres the code again so you dont have to scroll back up.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Character1AnimControl : MonoBehaviour {
static Animator anim;
public float speed = 2.0f;
public float rotationspeed = 75.0f;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
float translation = Input.GetAxis("Vertical")*speed;
float rotation = Input.GetAxis("Horizontal")*rotationspeed;
translation *= Time.deltaTime;
rotation *= Time.deltaTime;
transform.Translate(rotation, 0, -translation);
transform.Rotate(0, rotation, 0);
if (translation != 0)
{
anim.SetBool("IsWalking", true);
}else{
anim.SetBool("IsWalking", false);
}
}
}
Sorry, Iāve been thinking about this for a while, but havenāt come up with a good solution.
Itās not the first time Iāve been stumped by this sort of āissueā
It seems simple, but whenever I try to implement it, I cannot. lol
One thing that comes to mind is having a āwalk left/rightā animation. That is what I had always planned to use, had I ever gotten around to doing that. With that, my theory was that regular walking (forward or forward + any turning, for rotation) would be on thing. Then, walking left/right/backwards could have a seperate animation , all the while keeping my rotation unchanged. That seems simple in theoryā¦
I was trying to use a separate Vector3 (empty game object or stored vector3) to use as my āforward orientationā just now, but came up short. It sounds plausible, butā¦
Anyways, this is incomplete and may be similar to what you already have:
left/right will only āmoveā the player if theyāre already moving forward or backward.
Sorry, maybe someone else can help in a better way. Iād be curious/interested to learn a solution to this, too
I tried a bit more.
I used an empty game object as a parent.
In my code, the variable ācapsā is the main (child) object.
float v = Input.GetAxis("Vertical") * speed * Time.deltaTime;
float h = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
Vector3 move = transform.forward * v + transform.right * h;
move = Vector3.ClampMagnitude(move, speed * Time.deltaTime);
if (v != 0 && h != 0)
transform.rotation *= Quaternion.Euler(0, h*15, 0); // alter '15' to whatever feels comfortable for rotation speed.
else if (v > 0)
caps.transform.localRotation = Quaternion.identity;
else if (v < 0)
caps.transform.localRotation = Quaternion.Euler(0, -180, 0);
else if (h != 0)
{
if (h < 0) caps.transform.localRotation = Quaternion.Euler(0, -90, 0);
else caps.transform.localRotation = Quaternion.Euler(0, 90, 0);
}
transform.position += move;
Could look a little nicer, maybe, not sure⦠However, itās working decently well, as far as I can tell.
Let me know if itās any good with your character
a quick fix from your original code, take note of lines 26 and 27:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Character1AnimControl : MonoBehaviour {
static Animator anim;
public float speed = 2.0f;
public float rotationspeed = 75.0f;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
float translation = Input.GetAxis("Vertical")*speed;
float rotation = Input.GetAxis("Horizontal")*rotationspeed;
translation *= Time.deltaTime;
rotation *= Time.deltaTime;
transform.Rotate(0, rotation, 0);//moved this line to before movement
transform.Translate(transform.TransformDirection(translation));//movement is now moving based on rotation
if (translation != 0)
{
anim.SetBool("IsWalking", true);
}else{
anim.SetBool("IsWalking", false);
}
}
}
However, this is very car-like behavior and probably far from what you are intending to do. Just note that in games, rotation and movement is usually separated from each other for the sake of control, the linkage between them is an illusion.
That might come off as rude, but you are sure that you havenāt set any funny values for the Input Axis in the Editor, right? Because @Laperens script looks fine.
Could you explain what itsnāt working?
Its way closer, but no instead of moving left and right when I press W and S, he move a little diagonal but its closer. Also why when I turn it seems to be rotating around a axis not the center of his body?