- Home /
How to move a ball "faster" looking from a Camera
Hi everybody, here it is my problem. I'm trying to move my ball correctly but it seems there is something wrong in my code. I have my ball and behind it there is my Main Camera. I used this code to move my ball in relation of the view of my Main Camera:
public Vector3 forward;
public Vector3 right;
public Vector3 moveDirection;
void Update () {
forward = GameObject.Find("Main Camera").transform.TransformDirection(Vector3.forward);
forward = forward.normalized;
right = new Vector3(forward.z, 0, -forward.x);
h = Input.GetAxis("Horizontal");
v=Input.GetAxis("Vertical");
moveDirection = (h * right + v * forward)*speed ;
rigidbody.AddForce(moveDirection);
}
But the movement is really "slow". I need A LINEAR MOVEMENT. Not a force but I don't know how to apply it. Also when my ball is in air it falls down very slowly. What I'm trying to do is to have a movement like the one in this video: http://kotaku.com/5802589/inmomentum-is-all-about-the-joys-of-movement I suppose that the "addForce" call is a wrong way to do that. If I add a force to my ball and I want to let it change direction, I have to counteract the force acting on my ball and I don't want this. I know that in that video I don't see the Player from behind but the Camera corresponds with the Player view. I don't want this in my game as I explained you before. In my game I have allready put the MouseOrbit script to my Main Camera. What I want is well shown in that video. Can you please help me? Thank you very much!
I watched a portion of your video. I'm not what motion you are talking about. If it is the motion of the camera, then it seems very similar to what is implemented by the example example script for CharacterController.$$anonymous$$ove().
I just tried that code but it doesn't work.. I mean: 1)according to that video there is a camera who can rotate ($$anonymous$$ouseOrbit) and the player goes where the camera points.. using only the CharacterController.$$anonymous$$ove() it doesn't work because the player movement respect the original coordinates. But I want that he moves according to Camera Coordinates.. as in any normal game.. 2) $$anonymous$$y Camera bounce continuously.. there is nothing more added to my Camera.. only the $$anonymous$$ouseOrbit
I'm confused about what you want. If you want exactly what is in that video, remove $$anonymous$$ouseOrbit from the camera and attach the character controller and the example script to the camera. If you want a 3rd person game, put the CharacterController and the example script on the player in the scene. Then attach the the standard SmoothFollow script to the camera.
I want a 3rd person game but I want to orbit my camera.. And if I turn right my Camera and press W to move my player, this one has to go where the camera is looking.. But he uses always the original coordinates in that way. Beginning: if I press W the ball moves long my Z axes.. - I turn my camera of 90° on the right for example - If I press W now my player should move long X axis - But no! It keeps moving long the original Z axes And I don't want this! So what I want is: 1) a 3rd person game 2) a mouseOrbit Camera 3) a player$$anonymous$$ovement like in that video
I hope to have been clear this time :S Sorry for my english! Thank you again!
Separate out the motion of the player from the camera motion. Solve the player motion first. Looking at your video indicates that a CharacterController with the script that I reference above gets you 80% of the way there. You will need to change the input to use keys (Input.Get$$anonymous$$ey() most likely), and to make left and right turn rather than "slide," but the feel is the same. Once you get that down, then add the rest.
Answer by robertbu · Mar 09, 2013 at 05:06 AM
Here's a new script. The transForward variable needs to be initialized by dragging the camera on top of this variable. The script then utilizes the camera to calculate the forward vector for the object controlled by this script. Just for my own testing, it uses the mouse buttons as well as the arrow keys for forward and backward movement.
using UnityEngine;
using System.Collections;
public class CharHandler : MonoBehaviour {
public Transform transForward; // Transform to use for forward movement
public float speed = 1000.0f;
public float jumpSpeed = 11.0f;
public float gravity = 20.0f;
private Vector3 moveDirection = Vector3.zero;
private CharacterController controller;
void Start()
{
if (transForward == null)
{
Debug.Log ("trans not setup");
transForward = transform;
}
controller = GetComponent<CharacterController>();
}
void Update() {
Vector3 forward = Vector3.Cross (transForward.right, Vector3.up);
forward.Normalize();
transform.LookAt (transform.position + forward);
if (controller.isGrounded) {
moveDirection = Vector3.zero;
if (Input.GetKey (KeyCode.UpArrow) || Input.GetMouseButton (0))
moveDirection += forward * speed * Time.deltaTime;
if (Input.GetKey (KeyCode.DownArrow) || Input.GetMouseButton (1))
moveDirection += -forward * speed * Time.deltaTime;
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}
Well, you really helped me for this question. Thank you a lot for everything you did for me! You script works well. I just needed to do some changes to make it works even when it jumps (I wanted to be able to move even when I was jumping) and it was not a problem. Unfortunately I just realized that I really need a Sphere Collider for my player. This is why I need it: http://www.youtube.com/watch?v=W$$anonymous$$WGAj6CCgE&feature=youtu.be That thing is strange.. for that reason I need a more realistic movement. I tried to apply the Sphere Collider having at the same time the Character Controller but unfortunately it doesn't work. So, thank you really a lot for everything you did for me!:)
I'm sure this is fixable. I don't know character controller well enough to tell you how to fix it. I have some ideas. Google() the list for "stuck character controller" or similar searches and see what you find. If you don't find a solution, post the screen shot of the ball stuck to the side along with a new question. $$anonymous$$y best guess for a solution is to raycast down. If the ray does not hit a platform just under the ball, ignore the isGrounded flag and allow the ball to go down.
Good. If you tell me that that's fixable I will try to find a solution or I will post a new question. Thank you again!
Your answer
Follow this Question
Related Questions
My game is laggy, but there are only a ball and some flat platforms. 3 Answers
"Ballance" Type ball game Problem. 0 Answers
Slow camera move speed? 2 Answers
The sphere goes Slow 1 Answer