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
1
Question by outsiderman411 · Jan 29, 2012 at 08:16 PM · movementcharactercontrolstrafe

Unity Strafing Script

I am looking for a script that will allow the character to run and jump, but not rotate. Instead I want the character to be able to move across the screen while still facing forward. Can anyone help please?

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by aldonaletto · Jan 29, 2012 at 08:21 PM

Use the example script from CharacterController.Move: it moves left/right/front/back and jumps, but do not rotate. NOTE: This script is intended to be used with a CharacterController, obviously.

Comment
Add comment · 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
0

Answer by Davoid ZalaX · Dec 06, 2014 at 06:41 AM

Here, Try borrowing from this 3rd person character controller.

 #pragma strict
 
 var rotataionSpeed : float = 10;
 var walkSpeed : float = 7;
 var gravity : float = 50;
 private var yRot : float;
 var body : Transform;
 
 
 function Update () {
     var Controller : CharacterController = GetComponent(CharacterController);
     var vertical : Vector3 = transform.TransformDirection(Vector3.forward);
     var horizontal : Vector3 = transform.TransformDirection(Vector3.right);
     var height : Vector3 = transform.TransformDirection(Vector3.up);
     
     if(Input.GetKeyDown("space")){
     Jump();
     }
     
 if(Input.GetAxis("Vertical") || Input.GetAxis("Horizontal")){
     animation.CrossFade("walk", 0.2);
     animation["walk"].speed = walkSpeed/10;
     Controller.Move((vertical*(walkSpeed*Input.GetAxis("Vertical")))*Time.deltaTime);
     Controller.Move((horizontal*(walkSpeed*Input.GetAxis("Horizontal")))*Time.deltaTime);
 }else{
     animation.CrossFade("idle", 0.2);
 }
 
 if(Input.GetAxis("Mouse X")){
     yRot += 10*Input.GetAxis("Mouse X");
     }
     transform.rotation = Quaternion.Euler(0, yRot, 0);
     
     Controller.Move(height*gravity*Time.deltaTime);
 }
 function LateUpdate(){
  // Rotate the Character to match the direction he/she is going
   if(Input.GetAxis("Vertical") == 0){
     if(Input.GetAxis("Horizontal") > 0){
       body.localEulerAngles.y = 180;
     }else if(Input.GetAxis("Horizontal") < 0){
       body.localEulerAngles.y = 0;
     }
   }else if(Input.GetAxis("Vertical") > 0){
     if(Input.GetAxis("Horizontal") > 0){
       body.localEulerAngles.y = 135;
     }else if(Input.GetAxis("Horizontal") < 0){
       body.localEulerAngles.y = 45;
     }
   }else if(Input.GetAxis("Vertical") < 0){
     if(Input.GetAxis("Horizontal") == 0){
       body.localEulerAngles.y = -90;
     }else if(Input.GetAxis("Horizontal") > 0){
       body.localEulerAngles.y = -135;
     }else if(Input.GetAxis("Horizontal") < 0){
       body.localEulerAngles.y = -45;
     }
   }
 }
 function Jump(){
     animation.CrossFade("jump_pose", 0.2);
     gravity = 3;
     yield WaitForSeconds(0.5);
     gravity = -10;
 }
 
Comment
Add comment · 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

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

6 People are following this question.

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

Related Questions

How to make camera position relative to a specific target. 1 Answer

Player slows down when hitting an incline 0 Answers

C# Movement Script 3 Answers

Kinect issue with Character Movement while in flight 0 Answers

Network - Character Controls 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