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 Scarfo76 · Sep 16, 2012 at 01:37 AM · controls

Character Controls like the game "Fable"

these are my current Character Controls

 //moving around
 var speed : float = 15.0;
 var rotateSpeed : float = 3.0;
 
 //shooting
 var bulletPrefab:Transform;
 
 //dying
 static var dead = false;
 
 //getting hit
 var tumbleSpeed = 800;
 var decreaseTime = 0.01;
 var decayTime = 0.01;
 static var gotHit = false;
 private var backup = [tumbleSpeed, decreaseTime, decayTime];
 
 function LateUpdate()
 {
     if(dead)
     {
         transform.position = Vector3(-40,4,0);
         gameObject.Find("Main Camera").transform.position = Vector3(-40,4,0);
         dead = false;
     }
     
     if(gotHit)
     {
         if(tumbleSpeed < 1)
         {
             //we're not hit anymore ... reset and get back into the game
             tumbleSpeed = backup[0];
             decreaseTime = backup[1];
             decayTime = backup[2];
             gotHit = false;
         }
         else
         {
             //we're hit.. spin character around
             transform.Rotate(0,tumbleSpeed*Time.deltaTime,0,Space.World);
             tumbleSpeed = tumbleSpeed - decreaseTime;
             decreaseTime += decayTime;
         }
     }
 }
 
 //function OnControllerColliderHit(hit : ControllerColliderHit)//onTriggerEnter | collider
 function OnTriggerEnter( hit : Collider )
 {
     if(hit.gameObject.tag == "fallout")
     {
         dead = true;
         //subtract life here
         HealthControl.LIVES -= 1;
     }
     
     if(hit.gameObject.tag == "enemyProjectile")
     {
         gotHit = true;
         HealthControl.HITS += 1;
         Destroy(hit.gameObject);
     }
 }
 
 function Update ()
  {
     var controller : CharacterController = GetComponent(CharacterController);
 
     // Rotate around y - axis
     transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
     
     // Move forward / backward
     var forward : Vector3 = transform.TransformDirection(Vector3.forward);
     var curSpeed : float = speed * Input.GetAxis ("Vertical");
     
     controller.SimpleMove(forward * curSpeed);
     
     if(Input.GetButtonDown("Fire1"))
     {
         var bullet = Instantiate(bulletPrefab,
                                 transform.Find("spawnPoint").transform.position,
                                 transform.rotation);
         bullet.tag = "playerProjectile";
         bullet.rigidbody.AddForce(transform.forward * 6000);
     }
 }
 @script RequireComponent(CharacterController)
Comment
Add comment · Show 5
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 AlucardJay · Sep 16, 2012 at 03:24 AM 0
Share

What is the question exactly? What is wrong with this script, what do you want it to do? (some of us have never played 'Fable' )

avatar image Scarfo76 · Sep 16, 2012 at 06:50 PM 0
Share

sorry about that, im not quite sure how to explain the controls for fable. it is a third person RPG and i just wish to have better 3rd person Character controls than the default ones on unity. i would just like much more control over how i move.

i'm sorry if this isn't very helpful :/

avatar image DesiQ · Sep 16, 2012 at 10:38 PM 0
Share

Nope, not helpful. What specifically is it about the default character controller that is lacking and that you want to improve?

avatar image Scarfo76 · Sep 17, 2012 at 01:59 AM 0
Share

i'd like to be able to jump run walk i want the character to FACE the direction im going and have to camera turn the direction he is facing. as well as have the ability to use two different type of weapons or powers. and have left click primary right click secondary. that my not be exactly like fable but i havent played it on pc in years. i just want more control with my character and not just A and D rotates left and W forward S backwards. if i press S i want him to face backwards and go that way, not just transform backwards. i am not sure how much help this is ...

avatar image AlucardJay · Sep 17, 2012 at 03:31 AM 0
Share

That's another swing-and-a-miss ! This kind of vague question usually gets left unanswered, but I'm feeling helpful and have written an answer that may help =]

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by AlucardJay · Sep 17, 2012 at 03:34 AM

I still have no clue what a 'Fable' camera is, but decided to make a basic player controller. The character moves with WASD or arrow keys, to turn the character 180 degrees, press Q. The mouse horizontal rotates the character. Check the screenshot for the hierarchy on the gameObjects, follow the instructions on setting up an empty gameObject that will be the camera's position.

  • Create a basic scene, attach the below script to the Player Character

how to set up the CameraHolder object used in the script :

  • set the camera in the desired position

  • create an empty gameObject, name it CameraHolder

  • make the empty CameraHolder a child of the camera

  • set the Transform andd Rotation of CameraHolder to 0 in the Inspector

  • Now drag CameraHolder out of the camera and make it a child of the Player Character

  • CameraHolder is now set in the correct position

Here is the Script :

 #pragma strict
 
 public var theCamera : Transform;
 
 private var movementSpeed : float = 5.0;
 private var rotationSpeed : float = 5.0;
 private var currentRotation : float = 0.0;
 
 private var myTransform : Transform;
 private var cameraHolder : Transform;
 
 
 function Start() 
 {
     myTransform = this.transform;
     cameraHolder = transform.Find( "CameraHolder" );
 }
 
 function Update() 
 {
     // get inputs
     var inputX : float = Input.GetAxis( "Horizontal" );
     var inputY : float = Input.GetAxis( "Vertical" );
     var inputR : float = Mathf.Clamp( Input.GetAxis( "Mouse X" ), -1.0, 1.0 );
     // press 'Q' to turn 180 degrees
     if ( Input.GetKeyDown(KeyCode.Q) )
     {
         currentRotation += 180.0;
     }
     
     // get current position and rotation, then do calculations
     // position
     var moveVectorX : Vector3 = myTransform.forward * inputY;
     var moveVectorY : Vector3 = myTransform.right * inputX;
     var moveVector : Vector3 = ( moveVectorX + moveVectorY ).normalized * movementSpeed * Time.deltaTime;
     
     // rotation
     currentRotation = ClampAngle( currentRotation + ( inputR * rotationSpeed ) );
     var rotationAngle : Quaternion = Quaternion.Euler( 0.0, currentRotation, 0.0 );
     
     // update Character position and rotation
     myTransform.position = myTransform.position + moveVector;
     myTransform.rotation = rotationAngle;
     
     // update Camera position and rotation
     theCamera.position = cameraHolder.position;
     theCamera.rotation = cameraHolder.rotation;
 }
 
 
 function ClampAngle( theAngle : float ) : float 
 {
     if ( theAngle < -360.0 )
     {
         theAngle += 360.0;
     }
     else if ( theAngle > 360.0 )
     {
         theAngle -= 360.0;
     }
     
     return theAngle;
 }

alt text


fablechar.png (451.9 kB)
Comment
Add comment · Show 17 · 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 AlucardJay · Sep 18, 2012 at 10:02 AM 0
Share

Hello. Did this script work, or did you find the suggestion useful? Please post a comment or mark an answer, for future reference by other people searching this 'site.

This is the usual answer for questions like this, so I think this script is a bonus : http://answers.unity3d.com/questions/309842/dungeon-defenders-camera-and-control.html

avatar image Scarfo76 · Sep 19, 2012 at 03:19 AM 0
Share

@jay $$anonymous$$ay thanks, I have tried this but 1) i get the error at the bottom. 2) the capsule doesnt move with the camera

NullReferenceException UnityEngine.Transform.get_position () (at C:/BuildAgent/work/d9c061b1c154f5ae/Runtime/ExportGenerated/Editor/UnityEngineTransform.cs:19) NewChar.Update () (at Assets/$$anonymous$$yStuff/1$$anonymous$$yScripts/NewChar.js:46)

avatar image AlucardJay · Sep 19, 2012 at 03:27 AM 0
Share

The script goes on the player. $$anonymous$$ake sure you have dropped the camera into theCamera in the inspector.

I have made a package you can import and check how I have set it up : http://www.alucardj.net16.net/unityanswers/FableChar.unitypackage

In Unity, browse to Assets > Import Package > Custom Package , then locate where you downloaded the package.

avatar image AlucardJay · Sep 20, 2012 at 02:28 PM 1
Share

Change the Update to this :

 function Update() 
 {
     // get inputs
     var inputX : float = Input.GetAxis( "Horizontal" );
     var inputY : float = Input.GetAxis( "Vertical" );
     
     // get current position, then do calculations
     var moveVectorX : Vector3 = theCamera.forward * inputY;
     var moveVectorY : Vector3 = theCamera.right * inputX;
     var moveVector : Vector3 = ( moveVectorX + moveVectorY ).normalized * movementSpeed * Time.deltaTime;
     
     
     // update Character position
     myTransform.position = myTransform.position + Vector3( moveVector.x, 0.0, moveVector.z );
     
     // and rotation
     myTransform.LookAt( myTransform.position + Vector3( moveVector.x, 0.0, moveVector.z ) );
     
     
     // Firing
     if ( Input.GetButtonDown("Fire1") )
     {
         var bullet = Instantiate( bulletPrefab, transform.Find("spawnPoint").transform.position, transform.rotation );
         bullet.tag = "playerProjectile";
         bullet.rigidbody.AddForce( transform.forward * 6000 );
     }
     
 }

remove : @script RequireComponent(CharacterController)

in if (dead) , you now have a reference to the main camera, so change that line to :

 theCamera.position = Vector3(-40,4,0);

But this camera part may be overriden by the commands driving the camera. And the tumbling part I am worried about the char breaking. If you store the rotation before tumbling, then when respawning just use that rotation and give it back to the char.

avatar image AlucardJay · Sep 20, 2012 at 02:30 PM 1
Share

Oops, don't forget to add this :

 public var theCamera : Transform;
 public var movementSpeed : float = 8.0;
 private var myTransform : Transform;
 
 function Start() 
 {
     myTransform = this.transform;
 }
 
 function ClampAngle( theAngle : float ) : float 
 {
     if ( theAngle < -360.0 )
     {
         theAngle += 360.0;
     }
     else if ( theAngle > 360.0 )
     {
         theAngle -= 360.0;
     }
     
     return theAngle;
 }
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

11 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 avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Mech Movement/Camera Help 0 Answers

Toggling a basic graphical GUI overlay using the escape key. 3 Answers

iPhone joystick? 0 Answers

Jump Speed 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