- Home /
Character Movement not moving how i want
Hi there and thanks for looking. I have some code right now that kinda works how i want. I want it to strafe left and right go back and forth. Things is when i press W he will always go forward no matter where my camera is facing. I want it to be where he fill go in the direction my camera is facing.
I am trying to get this controller to be like Ratchet And Clanks character controller so any help on how to do this or just help me with the above would be amazing.
I am trying to learn movements as they confuse the shizzle out of me. I do understand code very well but when it comes to this kinda thing it just goes over my head so any help with this would also be amazing.
Here is the code
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
private CharacterController controller;
private Vector3 moveDirection;
private Vector3 forward;
private Vector3 right;
public float speed;
// Use this for initialization
void Start () {
controller = gameObject.GetComponent<CharacterController>();
moveDirection = Vector3.zero;
right = Vector3.zero;
forward = Vector3.zero;
}
// Update is called once per frame
void Update () {
// anhance this but leanr more as you go forward. Learn each statement so you know what it is
forward = Vector3.forward;
right = new Vector3(forward.z, 0, -forward.x);
float horizontalInput = Input.GetAxisRaw("Horizontal");
float verticalInput = Input.GetAxisRaw("Vertical");
Vector3 targetDirection = horizontalInput * right + verticalInput * forward;
moveDirection = Vector3.RotateTowards(moveDirection, targetDirection, 200 * Mathf.Deg2Rad * Time.deltaTime, 1000);
Vector3 movement = moveDirection * Time.deltaTime * speed ;
controller.Move(movement);
Mathf.Clamp(0,0,0);
float lookRot = Input.mousePosition.z;
if (targetDirection != Vector3.zero)
{
Vector3 relativePos = transform.position - Camera.main.transform.position;
float x, y, z;
x = relativePos.x;
y = 0;
z = relativePos.z;
Vector3 look = new Vector3(x, y, z);
Quaternion rotation = Quaternion.LookRotation(look * Time.deltaTime * 2);
transform.rotation = rotation;
}
}
}
Answer by Khada · Sep 19, 2012 at 11:45 AM
Replace things like:
forward = Vector3.forward;
right = new Vector3(forward.z, 0, -forward.x);
With:
forward = transform.forward;
right = transform.right;
Also, use the inverse of these for back/left:
backwards = -transform.forward;
left = -transform.right;
'Vector3.forward' will always return the same value, in this case (0,0,1). This means that 'transform.position += Vector3.forward;' will always move your object down the Z axis. As such, the direction variables within the Vector3 struct are always treated as world/global directions.
When you want the relative forward direction of an object (emphasis on relative), you obtain this from the objects transform as these values are unique to the object and so are contained within the objects unique transform information. 'transform.forward' will always be the forward facing direction of the object in question.
so i could say something like Direction = player.transform.forward and this will make something go in the players forward direction? If so the main thing that confuses the crap outa me are rotations also. I mean Quaternion.Euler WTF!!!:(
The common way of moving something in a forward direction is this:
transform.position += transform.forward * Time.deltaTime * m_Speed;
//where speed if how fast you want it to travel.
I get it so i am adding 1 to my current position in the forward direction then getting my speed. I get all the Time.deltaTime and stuff i just feel like a noob right now lol i knew this day would come though where i need to learn this. Thanks mate if you have any other tips on directions maybe some little secrets or good ways of thinking on how to do it including rotations would be awesome lol :):):) Chancing my luck here you got Skype?
Rotation is a whole different ball game and a question unto itself. I could give you my skype, but then how I would I earn that sweet, sweet karma? And as a $$anonymous$$cher, I'm well disciplined at not giving personal contact info out. If I did, I wouldn't have time to breath. Just throw your questions up on here when you get stuck. Cheers :)
Answer by john-essy · Sep 19, 2012 at 05:10 PM
Yea i get what you are saying thanks man, Thing is i still don't understand how it all works all i know is it kinda works. I want to know as much as possible with all this movement lark :P
I'll add some info to my answer. You should post this as a comment though, not an answer.
Your answer
Follow this Question
Related Questions
How can I rotate a character 180 degrees by D, and -180 by A 2 Answers
Localscale-flipped 2D character retains original rotation since 5.4 2 Answers
How to be tangent with the ground without a character controller? 2 Answers
How to get my character to collide with objects 1 Answer
character controller height transition 0 Answers