Hi guys,
I am new to unity and new to scripting. I am trying to make a simple game but am having trouble with rotation when my character is moving. It is a third person camera view, the character is the parent of the camera so it follows him. Whenever I turn when he is moving, the character model and camera dont seem to turn as fast as they should be. So after I turn, when I try to move him forward again, he ends up moving to the side or at an angle. I have the movement speed at 10 and the rotation at 25. Here is my script:
var moveSpeed : float;
var rotateSpeed : float;
var jumpForce : float;
function Update () {
if(Input.GetButton(“Forward”))
{
transform.Translate(transform.forward * moveSpeed * Time.deltaTime);
}
if(Input.GetButton(“Backward”))
{
transform.Translate(-transform.forward * moveSpeed * Time.deltaTime);
}
if(Input.GetButton(“Right”))
{
transform.Rotate(transform.up * rotateSpeed * Time.deltaTime);
}
if(Input.GetButton(“Left”))
{
transform.Rotate(-transform.up * rotateSpeed * Time.deltaTime);
}
if(Input.GetButtonDown(“Jump”))
{
animation.Play(“Jump”);
rigidbody.AddForce(Vector3.up*jumpForce);
}
}
There’s nothing in this script that could account for the symptom you describe. That is, somehow you end up in a situation where movement forward instead moves him sideways.
If you attach this script to a brand new cube or something, and comment out the rigidbody line near the end, I think you’ll find it works just fine. So, something else is going on in your real case. Look at what other scripts are attached to the same object, or to parents or children of that object. The problem almost certainly lies there, in the interaction of those multiple scripts.
HTH,
The same thing happened with the cube
is there something wrong with the way I am making it rotate? There are no other scripts attached.
why dont you yous getaxis ? its better (i think) and if not and have that much ifs better use switch statement
what would be an example code for using getaxis to rotate? sorry I am very new to all this! thanks in advance!
private float _Hor;
void FixedUpdate()
{
_Hor = Input.GetAxis ("Horizontal");
if (_Hor > 0)
{
transform.Rotate(Vector3.up * 100 * Time.deltaTime);
}
else if (_Hor < 0)
{
transform.Rotate(-Vector3.up * 100 * Time.deltaTime);
}
}
this is my code for rotate my character (with ‘a’ and ‘d’ buttons)
any idea what it would be in javascript?
If anyone has some time on their hands to help me out, my skype is mitch.johnson52
I can share my screen and show you whats going on. Thanks!
Its almost the same on JavaScript. And I prefer to you if you can’t convert it by yourself you better spend some time mastering a programing language and unity documentation and life will be easier
OK, sorry, I should have seen this… you’re using Transform.Translate, which I rarely use, and I forgot that by default the translation is in local coordinates, i.e. relative to the transform’s own rotation. But you’re already converting your movement direction from local to world coordinates by using transform.forward, so basically it’s getting converted twice, and ends up with the wrong answer.
So the easiest fix, if you like to use Transform.Translate, is to simply give it a local direction. That is, forward is always in the direction of <0, 0, 1>, or Vector3.forward (which is just a shortcut for that particular vector). Here’s my version of your script:
using UnityEngine;
using System.Collections.Generic;
public class MoveTest : MonoBehaviour {
float moveSpeed = 10;
float rotateSpeed = 25;
void Update() {
if (Input.GetAxisRaw("Vertical") > 0) {
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
}
if (Input.GetAxisRaw("Vertical") < 0) {
transform.Translate(-Vector3.forward * moveSpeed * Time.deltaTime);
}
if (Input.GetAxisRaw("Horizontal") > 0) {
transform.Rotate(Vector3.up * rotateSpeed * Time.deltaTime);
}
if (Input.GetAxisRaw("Horizontal") < 0) {
transform.Rotate(-Vector3.up * rotateSpeed * Time.deltaTime);
}
}
}
Have fun!