- Home /
Character Controller
I have a character controller, and I wan't it to launch in the direction of the camera, kinda like "Rigidbody.AddForce" but, I can't use a rigid body controller, because it stops the forward movement... :(
So, is there any way I could do this with the character controller, that comes with unity? :)
Answer by dsada · Jul 16, 2014 at 01:38 PM
use its SimpleMove function. You need to add a Vector3 that indicates the direction and the speed as well. Here is the Unity's example script:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(CharacterController))]
public class ExampleClass : MonoBehaviour {
public float speed = 3.0F;
public float rotateSpeed = 3.0F;
void Update() {
CharacterController controller = GetComponent<CharacterController>();
transform.Rotate(0, Input.GetAxis("Horizontal") * rotateSpeed, 0);
Vector3 forward = transform.TransformDirection(Vector3.forward);
float curSpeed = speed * Input.GetAxis("Vertical");
controller.SimpleMove(forward * curSpeed);
}
}
I wrote a simple code, and it didn't work. It only goes forward on the z axis, not the y axis! :(
Code:
using UnityEngine;
using System.Collections;
public class Pounce : $$anonymous$$onoBehaviour {
public float Force = 20;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
CharacterController controller = GetComponent<CharacterController>();
if(Input.GetButtonDown("Fire3")){
controller.Simple$$anonymous$$ove(transform.FindChild("Camera").TransformDirection(Vector3.forward) * Force);
}
}
}
Well the gravity applies to the character controller, so maybe that could be the problem. You would like to move the character upwards?
Yes in a way I would like to move the character upwards.
See, I'm making a game like "The Hidden" If you have played that? And I want to be able to launch my character like the hidden person in that game!
Well then you may want to use CharacterController.$$anonymous$$ove() ins$$anonymous$$d of Simple$$anonymous$$ove()
Your answer
Follow this Question
Related Questions
How to use AddForce on FPC 0 Answers
Physics AddForce reduced when 3 objects are colliding 1 Answer
Erratic Physics behaviour while testing in 2 computers 0 Answers
2D metroid boost ball 0 Answers