Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by rross46 · Mar 30, 2013 at 01:27 PM · movementdirectioncustom

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rross46 · Mar 31, 2013 at 09:45 AM 0
Share

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");
         }
 
     }
 }
 

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Mar 30, 2013 at 09:21 PM 0
Share

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.

avatar image rross46 · Mar 31, 2013 at 09:47 AM 0
Share

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!

avatar image rross46 · Mar 31, 2013 at 11:44 AM 0
Share

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);
         }
     }
 }
 
avatar image rross46 · Mar 31, 2013 at 11:46 AM 0
Share

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.

avatar image robertbu · Mar 31, 2013 at 01:51 PM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make a movement system that changes with your viewpoint but also lets you hit multiple keys at once to go diagonally? 0 Answers

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges