Question by
paulminyukuo · Jul 11, 2016 at 01:29 AM ·
rotationmovementcharactercontrollerquaternionbeginner
try to rotate a character controller when translating on z only
Hi Guys
I am trying to learn scripting in unity, and I am having a really hard time to get my character controller working.
I would like the rotate my character controller when translating on z only
My character prefab currently have
character controller
movement script i put together
using UnityEngine; using System.Collections;
public class movement : MonoBehaviour {
public float speed = 350.0F; public float jumpSpeed = 300.0F; public float gravity = 500.0F; private Vector3 moveDirection = Vector3.zero; void FixedUpdate() { CharacterController controller = GetComponent<CharacterController>(); if (controller.isGrounded) { moveDirection = new Vector3(Input.GetAxis("Vertical")*-1, 0, Input.GetAxis("Horizontal")); moveDirection = transform.TransformDirection(moveDirection); moveDirection *= speed; if (Input.GetButton("Jump")) moveDirection.y = jumpSpeed; } moveDirection.y -= gravity * Time.deltaTime; controller.Move(moveDirection * Time.deltaTime); Quaternion rot = new Quaternion(); rot.SetLookRotation(moveDirection); transform.rotation = rot; } }
Right now the character just turning in all kinds of weird direction
Can someone please let me know where I did wrong
Thanks
rotation-function.jpg
(241.7 kB)
Comment