My player object is a sphere and my controller script is not working. Moving forward and back is very irratic. Can somebody help me to fix it? I’m new to unity and programming. Here is my script.
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public Vector3 cameraFollowOffset = new Vector3(0,0,0);
public Camera followCamera;
private float yRotation;
public float speed;
void Start()
{
if (followCamera == null)
followCamera = Camera.main;
}
void Update()
{
followCamera.transform.position = transform.position + cameraFollowOffset;
if (Input.GetKeyDown (KeyCode.LeftArrow))
{
rigidbody.velocity = Vector3.zero;
rigidbody.angularVelocity = Vector3.zero;
yRotation -= 90;
transform.eulerAngles = new Vector3(0, yRotation, 0);
}
if (Input.GetKeyDown (KeyCode.RightArrow))
{
rigidbody.velocity = Vector3.zero;
rigidbody.angularVelocity = Vector3.zero;
yRotation += 90;
transform.eulerAngles = new Vector3(0, yRotation, 0);
}
}
void FixedUpdate()
{
if (Input.GetKeyDown (KeyCode.UpArrow))
{
rigidbody.AddRelativeForce (Vector3.forward * speed);
}
else if (Input.GetKeyDown (KeyCode.DownArrow))
{
rigidbody.AddRelativeForce (Vector3.back * speed);
}
}
}
That won’t work, because when i rotate my sphere (turn left/right) it doesn’t move forward in that direction. Allways moves forward in one direction with that code. Thanks anyway.
Well, that didn’t help much. I don’t know what i’m suppose to do with that. As i said, i’m total n00b in programming, scripting and Unity. I just started an course in programming three months ago. And i had absolutely no experience at all. Firts we had C++, then C# and now we started Unity. Maybe this just isn’t “my bag…” C++ and C# i get a little, because i managed to program Blackjack game with C# and WPF as a course sample, but i don’t seem to understand Unity. Well… It’s only been a week. Maybe i should start with something diffrent. Making “Kula World” maybe a little too much to start with That sphere movement is killing me…
Ok, Vector3 is the struct that represents the position / direction in 3d space.(x, y, z)
I recommend you to watch this tutorial provided by Unity, this explains the basics.
The Vector3(direction to move in) is current global. to change this relative to a certain
object you’ll have to do the following:
transform.TransformDirection(Vector3);
Inside the parameters you’ll put the Vector3 containing the global direction.
So now that you have the direction that you want to move in, we can add force.
You can either change the rigidbody’s velocity, or add force(for more realistic physics).
public ForceMode forceMode;
public float MoveForce = 4;
void Update() {
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Vector3 direction = this.transform.TransformDirection( new Vector3(h, 0, v) );
rigidbody.AddForce(direction * MoveForce, forceMode);
}
this will fix the direction relative to the gameObject that the script is attached to.
Thanks. I will try to rewrite my script tomorrow. It’s nice that someone is taking the time and effort to help me. I wish our teacher would actually teach us, how to use and code with Unity. Our teacher has so far just shown us what Unity can do, but not so much as actually teaching us in my opinion. Maybe later i’ll read some books and watch video tutorials when i’m not too busy. Actually, i have no plan to get work as gameprogrammer. The aim of this course for me is more in maybe getting a job in other programming, but unity and gaming is part of our course. I’m not that much into gaming, but doesn’t hurt to try some stuff.