- Home /
Rotation for movement cannot be set in script?
Hello. So in my playermovement script I have this line:
direction = transform.rotation * new Vector3( Input.GetAxis("Horizontal") , 0, Input.GetAxis("Vertical") );
And it doesn't move me in the direction that I'm facing. Why is this?
Answer by zharik86 · Sep 06, 2014 at 08:24 PM
In most cases player is directed by the face on an axis z. Then GetAxis define the new direction. Then write so(write on CSharp):
void Update() {
Vector3 relativeDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
Vector3 absoluteDirection = transform.rotation * relativeDirection;
transform.position += absoluteDirection * Time.deltaTime;
}
I hope that it will help you.
Alright this is my whole script:
using UnityEngine;
using System.Collections;
public class Player$$anonymous$$ovement : $$anonymous$$onoBehaviour {
/*
*
* This component is only enabled for "my player" (i.e. the character belonging to the local client machine)
*
*
*/
float speed = 10f;
float jumpSpeed = 6f;
Vector3 direction = Vector3.zero; // forward/back & left/right
float verticalVelocity = 0;
CharacterController cc;
Animator anim;
CapsuleCollider capsCollider;
float realCapsuleColliderHeight = 2f;
float jumpCapsuleColliderHeight = 1.2f;
// Use this for initialization
void Start () {
cc = GetComponent<CharacterController>();
anim = GetComponent<Animator>();
capsCollider = (CapsuleCollider)collider;
}
// Update is called once per frame
void Update () {
// WASD forward/back & left/right movement is stored in "direction"
Vector3 relativeDirection = new Vector3(Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
direction = transform.rotation * relativeDirection;
if(direction.magnitude > 1f) {
direction = direction.normalized;
}
anim.SetFloat("Speed", direction.magnitude);
// Handle Jumping
if(cc.isGrounded) {
anim.SetBool("Jumping", false);
//capsCollider.height = realCapsuleColliderHeight;
if(Input.GetButton("Jump")) {
verticalVelocity = jumpSpeed;
}
else {
verticalVelocity = -1f;
}
}
else {
anim.SetBool("Jumping", true);
//capsCollider.height = jumpCapsuleColliderHeight;
}
}
// FixedUpdate is called once per physics loop
// Do all $$anonymous$$OVE$$anonymous$$ENT and other physics stuff here.
void FixedUpdate () {
Vector3 dist = direction * speed * Time.deltaTime;
if(cc.isGrounded) {
}
else {
verticalVelocity += Physics.gravity.y * Time.deltaTime;
}
Debug.Log (verticalVelocity);
dist.y = verticalVelocity * Time.deltaTime;
cc.$$anonymous$$ove( dist );
}
}
It's only going forward in one direction.
@TrivialRoo The most part of a code at you is written for CharacterController. The first, delete your CapsuleCollider. Further, it isn't necessary to use function the FixedUpdate(). I will a little correct your script:
using UnityEngine;
using System.Collections;
public class Player$$anonymous$$ovement : $$anonymous$$onoBehaviour {
float speed = 10f;
float jumpSpeed = 6f;
Vector3 dir = Vector3.zero; // forward/back & left/right
float myGravity = 10.0f; //variable for gravity
CharacterController cc;
Animator anim;
void Start () {
cc = GetComponent<CharacterController>();
anim = GetComponent<Animator>();
//If you want, here you can change variable gravity
myGravity = Physics.gravity.y;
}
void Update() {
//Change direction only on ground
if(cc.isGrounded) {
anim.SetBool("Jumping", false);
//$$anonymous$$ost likely you expect to receive values +1 or-1, but magnitude returns vector length, therefore value always the positive.
//Therefore you need necessary to invent a certain condition
anim.SetFloat("Speed", direction.magnitude); //maybe wrong
Vector3 relativeDirection = new Vector3(Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
dir = this.transform.TransformDirection(relativeDirection);
dir = dir * speed;
if(Input.GetButton("Jump")) {
dir.y = jumpSpeed;
anim.SetBool("Jumping", true);
}
} else {
anim.SetBool("Jumping", true);
}
dir.y -= myGravity * Time.deltaTime; //Always apply gravity
cc.$$anonymous$$ove(dir * Time.deltaTime);
}
}
As that so.
Your answer
Follow this Question
Related Questions
Animator movement and mouse position problem 0 Answers
Player not rotating with camera 1 Answer
Player Move-Rotation 0 Answers
How do i rotate my player in moving direction in 2d? 0 Answers
transform.lookat snaping direction 0 Answers