- Home /
Custom movement script problems
Hi there. I am having some issues and have been looking through all the different answers/questions on the site, but haven't really found anything that hits the nail on the head for my problem.
I am pretty new to Unity and games programming (I know how to code, just not 3D games!). I was wanting to make my own custom movement script. I wanted to make a movement script where the L and R keys rotated the character left and right, the U key moves forward and D key moves backwards, with space for jump. I was also looking for the mouse look to only work when I hold down the right mouse key.
I had been looking at all kinds of examples, but what I notice is, using the standard assets FPSController, when I try to make my own scripts it is essentially overwritten by the generated scripts. When I disable them (the generated scripts), mines don't do anything. What do I need to do in order to allow the character to move using only my script? Must it have a motor attached to it?
If anyone can help me with the scripts and attaching them properly, I would be very grateful. I know the information is out there, but there is so much information it is a little overwhelming, a little help in narrowing it down would be appreciated.
Apologies for my code, I've added in some trace statements and some of the if statements are there for debug features. Basically, when I press Up, I become ungrounded and this stops me from moving (due to the if statement). I've messed up, and I aint too confident in 3D program$$anonymous$$g to be able to really spot where I am going wrong in the movement. When I press Up, I notice in the inspector window that the Y position changes slightly.
Any assistance would be appreciated.
using UnityEngine;
using System.Collections;
public class $$anonymous$$ovement : $$anonymous$$onoBehaviour
{
public float speed=10f;
public float rotationSpeed=10f;
public float jump=10f;
public float gravity=10f;
private Vector3 moveDirection=Vector3.zero;
private Vector3 rotateDirection=Vector3.zero;
public CharacterController control;
void Start()
{
Debug.Log ("Started");
control= GetComponent<CharacterController>();
moveDirection=new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
Debug.Log ("Direction - "+moveDirection);
if(!control.isGrounded)
{
Debug.Log ("Start $$anonymous$$ethod - Player isn't on ground");
Debug.Log ("Start $$anonymous$$ethod - Attempting to ground player");
moveDirection.y -= gravity;
control.$$anonymous$$ove (moveDirection*Time.deltaTime);
}
if(control.isGrounded)
{
Debug.Log ("Start $$anonymous$$ethod - Player is on ground");
}
}
void Update()
{
if(Input.Get$$anonymous$$ey ($$anonymous$$eyCode.LeftArrow))
{
Debug.Log ("Update $$anonymous$$ethod - Left has been pressed");
Debug.Log ("Update $$anonymous$$ethod - Attempting to rotate left");
rotateDirection=new Vector3(0,-1,0);
transform.Rotate (rotateDirection);
}
if(Input.Get$$anonymous$$ey ($$anonymous$$eyCode.RightArrow))
{
Debug.Log ("Update $$anonymous$$ethod - Right has been pressed");
Debug.Log ("Update $$anonymous$$ethod - Attempting to rotate Right");
rotateDirection=new Vector3(0,1,0);
transform.Rotate (rotateDirection);
}
if(control.isGrounded)
{
moveDirection=new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection*=speed;
Debug.Log ("Update $$anonymous$$ethod - Direction - "+moveDirection);
Debug.Log ("Update $$anonymous$$ethod - Is Grounded");
if(Input.Get$$anonymous$$ey ($$anonymous$$eyCode.UpArrow))
{
Debug.Log ("Update $$anonymous$$ethod - Up has been pressed");
Debug.Log ("Update $$anonymous$$ethod - Attempting to move forward");
if(moveDirection!=Vector3.zero)
{
Debug.Log ("Update $$anonymous$$ethod - Direction is not zero");
transform.forward=Vector3.Normalize (moveDirection);
moveDirection=transform.forward*speed;
moveDirection.y -= gravity * Time.deltaTime;
control.$$anonymous$$ove (moveDirection*Time.deltaTime);
}
if(moveDirection==Vector3.zero)
{
Debug.Log ("Update $$anonymous$$ethod - Direction is zero!!!");
}
}
}
if(!control.isGrounded)
{
Debug.Log ("Update $$anonymous$$ethod - Is not grounded");
}
}
}
Answer by robertbu · Mar 30, 2013 at 01:40 PM
The character controller script is meant to be driven my a second script. To get you started, use Component/Physics/Character Controller to attach a character controller. Then go to the reference for CharacterController.Move() or CharacterController.SimpleMove() and either of these two scripts to your character controller as well.
Neither script of these two scripts rotate the character using the L and R keys. The character just moves both horizontally and vertically. So you will have that change to make. If you have difficulty making the change, let me know which of the two scripts you are using, and I'll be glad to show you how to make the change.
In reference to your new code, don't post followup material as a new Answer. Now your questions is marked as having two answers, so far less people will look at it. You can convert your answer to a comment using the "more" dropdown below the answer.
I won't be at my desk for a good while, so it is a bit more difficult to sort out what is happening on this small screen. Some observations:
If you are not going to have a jump (which I see you removed), I'm not sure why you worry about being grounded (unless you will allow your controller to fall off things).
The start code dealing with grounding will not do what you expect. It will only move the controller down slightly. Just remove all the code there dealing with grounding.
The way you've structured your code, control.$$anonymous$$ove() is never executed if the controller is not grounded. Therefore if the controller for whatever reason becomes ungrounded (or starts out ungrounded), it will never fall due to "gravity." Look at the structure of the example code for CharacterController.$$anonymous$$ove(). $$anonymous$$ove() was called for every frame, and gravity was executed every frame even if no other movements were taken.
Your rotation code is the right idea. You need to include a rotationSpeed variable and multiple the result by Time.deltaTime. Also you can use the predefined Vector3.up and -Vector3.up.
rotationDirection = -Vector3.up rotationSpeed Time.deltaTime;
This will do a left rotation. 'rotationSpeed' will be degrees per second.
Hi mate, thanks for getting back. Apologies for the follow up answer ins$$anonymous$$d of comment. I will have a jump feature in, I was just focusing on ground movement at the moment. I shall implement the changes and get back, thanks!
Having real issues with jump now. I can move around now, I can also do a stationary jump. However when I jump, then press a directional key, it takes forever to land again, I essentially glide. If I press jump whilst pressing a directional key (up and down I mean, rotation is fine) I pretty much stutter. I need to make it so that when I am in the air, I don't move forward the same way. Here is what I have : using UnityEngine; using System.Collections;
public class $$anonymous$$ovement : $$anonymous$$onoBehaviour
{
public float speed=100f;
public float rotationSpeed=100f;
public float jump=20f;
public float g=15f;
private Vector3 moveDirection=Vector3.zero;
private Vector3 rotateDirection=Vector3.zero;
private bool jumping;
public CharacterController control;
void Start()
{
Debug.Log ("Started");
control= GetComponent<CharacterController>();
moveDirection=new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
jumping=false;
}
void Update()
{
Debug.Log ("On ground = "+control.isGrounded);
if(!control.isGrounded)
{
ground ();//method to re ground player
}
if(Input.Get$$anonymous$$ey ($$anonymous$$eyCode.LeftArrow))
{
rotateDirection = -Vector3.up * rotationSpeed * Time.deltaTime;
transform.Rotate (rotateDirection);
}
if(Input.Get$$anonymous$$ey ($$anonymous$$eyCode.RightArrow))
{
rotateDirection= Vector3.up * rotationSpeed * Time.deltaTime;
transform.Rotate (rotateDirection);
}
if(Input.Get$$anonymous$$ey ($$anonymous$$eyCode.UpArrow)&& !jumping)
{
moveDirection=new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.forward*Time.deltaTime;
moveDirection*=speed;
moveDirection.y -= g * Time.deltaTime;
control.$$anonymous$$ove (moveDirection*Time.deltaTime);
}
if(Input.Get$$anonymous$$ey ($$anonymous$$eyCode.DownArrow )&& !jumping)
{
moveDirection=new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = -transform.forward*Time.deltaTime;
moveDirection*=speed;
moveDirection.y -= g * Time.deltaTime;
control.$$anonymous$$ove (moveDirection*Time.deltaTime);
}
if(control.isGrounded && Input.GetButton ("Jump") && !jumping)
{
jumping=true;
if(jumping)
{
moveDirection=new Vector3(0,0,0);
moveDirection.y=jump;
moveDirection.y -= g * Time.deltaTime;
control.$$anonymous$$ove (moveDirection*Time.deltaTime);
jumping=false;
}
}
}
public void ground()
{
if (!control.isGrounded)
{
moveDirection.y -= g * Time.deltaTime;
control.$$anonymous$$ove (moveDirection*Time.deltaTime);
}
}
}
Just to clarify a few things, the ground() method was brought in because when I tried to have a constant gravity effect, the forward/backward movement was also being called, I don't know if my solution is the best, but it manages to work. In my jump code I set the movement vectors to 0, this is because the jump was not only jumping, but moving in the direction it is facing also, I just wanted it to jump when the jump key is pressed.
The code is fine as long as you are happy with it. A $$anonymous$$or nit: by convention function and class names start with capital letters so your function should be Ground(). The compiler won't complain, but folks on this list who read your code will have to work harder.
If this question is answered, you can you click the checkmark next to the answer to close it out.
Your answer
Follow this Question
Related Questions
how to make my character walk automatically and when pressing a button change the direction 2 Answers
Why does my character only dash forward and not in the moving direction? 1 Answer
Character facing wrong direction when moving vertically 0 Answers
How can I get my character to face the direction he is moving? 1 Answer