JCar on iPhone

Hello people.

I am in a project to develop a car racing game for iPhone (well yeah, there are lots out of there but this is just a project of mine). After sometime playing around with Unity3D, I can get a grasp on how to make a decent racing game on Unity. But I have some scripting problems with Unity iPhone. I found JCar script of C# from the wiki. Just by inserting a cube, some wheels and the game runs fine with JCar. So my question here is how to make controller script for iPhone? From Roll-a-Ball example, there’s a hint on how to control the movement through iPhone controller thing. But the example’s in Javascript. I am not a heavy programmer since I am more to multimedia person so I find it’s hard to understand some of the codes especially when Unity could understand both JS and CS. I also wanted to add GUI controller and others. Again, different examples with different programming languages made me dumbfounded.

I am actually on the trial mode and willing to pay for the full program after my trial ended. Unity itself is a great development tool for games especially on Mac, Windows and also browsers. Unity iPhone still at infant stage, I believe, with various tools from Unity applied on it. But real problems arise whenever examples have different sources like JS and CS. Plus less examples on Unity iPhone.

I hope someone can help me regarding my matters above. Thanks.

Sorry for bumping but really no one here can help me with iPhone controller to be applied in the JCar script? :frowning:

well, this is untested, but should work:

search for the following code:

steer = Input.GetAxis("Horizontal");
accel = Input.GetAxis("Vertical");
brake = Input.GetButton("Jump");

and replace it with:

steer = iPhoneInput.acceleration.y;
accel = iPhoneInput.acceleration.x;
brake = Input.GetMouseButtonDown(0);

i am pretty sure, that i have once again not the right axis (happens everytime :slight_smile: ), so try switching x/y/z for steer and accel until the car reacts how you want

// steer = Input.GetAxis(“Horizontal”);
// accel = Input.GetAxis(“Vertical”);
// brake = Input.GetButton(“Jump”);
steer = Mathf.Clamp(-2f * iPhoneInput.acceleration.y, -1f, 1f);
accel = Mathf.Clamp(4f * (-iPhoneInput.acceleration.z -0.5f), -1f, 1f);

Hi,
welcome here :smile:
this code i use for steering a car with the iPhone.
If you need more info about just do a search here with “iPhoneInput.acceleration”
and use Script Forum or IPhone Forum only and you will get all Code Info about.

is there any 3d racing project sample running on unity iPhone for i can learn?
i really newbie and i think it would be helping when learn from sample project.

thanks all

Hi:

I’m getting 2 errors when I load the JCar

1st error: SmoothFollow.js(42,49); Operator '+" cannot be used with left hand side of type "Object and a right hand side of a type “float”

also

2nd error: JMessages.cs(8,13) error CS*)25 : Parsing error

1st error

wantedRotationAngle += Input.GetAxis("Horizontal") * turnFromInput;

2nd error

List<JMessageData> messages;

Thank you,

Chepe[/code]

Got it to work. Great sample guys.

Chepe

How did you make it work?
I am trying what I have no idea.

Thanks in advance

Hi mefistody:

Well, I commented the errors out. And the second thing, is that you have to compressed all the textures to PVRTC. The editor crashes if I hit play, but it runs fine on the Iphone

Good luck!

Chepe

after commented the two errors it shows another errors:

  1. “Assets/GUI/Scripts/JMessages.cs(13,28): error CS1526: A new expression requires () or [ ] after type”

  2. The code “messages = new List();”

Any solution chepe?
thanks,

You can only use .NET 1.1 with Unity iPhone. That means you can’t use generics such as:

messages = new List<JMessageData>();

You’ll want to change that to:

messages = new List();

You will then need to do conversions everywhere you use messages to get JMessageData:

JMessageData data = (JMessageData)messages[i];

Enjoy!

Thanks Cedar, it really help.
Now the problem is the “terrain component” wont load in Unity iPhone but it works in Unity for Mac osx.
Any suggest? Thanks.

Well, you have two options:
Either you use a plane ( for testing ) or a mesh modelled in a modelling application

Although, if you want to use the terrain provided inside or jcar example, you will need to attach the terrain to .obj script found in the unity wiki and hit run: i will export a mesh equal to the terrain. Now grab the transform position offer the terrain in unity and open the jcar project in unityiphone, import the obj mesh and give it the same coordinTes or the terrain

Philip

Thanks Philip, would you like to provide me the wiki url please?

Yes, what I did is to remove the offending script, but that is a cheap fix.
Also, get rid of Unity Terrain and create a plane mesh or bring a static mesh from your favorite 3D app.

Thank you guys for the updates, I’m not great with pragma strict stuff or programming for that matter…

Chepe

Don’t know were to add

JMessageData data = (JMessageData)messages[i];

This is the JMessages.cs script

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class JMessages : MonoBehaviour {

	public GUISkin skin;
	List<JMessageData> messages;
	public string version;

	// Use this for initialization
	void Start () {
		messages = new List();
		ShowHelp();		
	}
	
	public void ShowHelp() {
		messages.Clear();
		AddMessage("r to reset car", 10);
		AddMessage("page up/page down to switch gear", 10);
		AddMessage("g toggles manual/automatic gears", 10);
		AddMessage("t cycles through all-, back- and frontdrive", 10);
		AddMessage("spacebar for brake", 10);
		AddMessage("arrows for steering, gas", 10);
		AddMessage("JCar: " + version, 60);
		AddMessage("press h to get short help", 60, 60);
	}
	
	public void AddMessage(string msg, float timeout) {
		float t = Time.time;
		JMessageData d = new JMessageData();
		d.str = msg;
		d.startTime = t;
		d.endTime = t + timeout;
		messages.Insert(0, d);
	}

	public void AddMessage(string msg, float delay, float timeout) {
		float t = Time.time;
		JMessageData d = new JMessageData();
		d.str = msg;
		d.startTime = t + delay;
		d.endTime = t + delay + timeout;
		messages.Insert(0, d);
	}

	void OnGUI() {
		if (skin != null) {
			GUI.skin = skin;
		}
		if (Input.GetKeyDown("h")) {
			ShowHelp();
		}
		
		JMessageData toRemove = null;
		if (messages.Count != 0) {
			GUILayout.BeginArea(new Rect(128 + 32, 16, 420, Screen.height - 32));
			float t = Time.time;
			foreach (JMessageData d in messages) {
				if (d.startTime <= t) {
					GUILayout.Label(d.str);
				}
				if (d.endTime < t) {
					toRemove = d;
				}
			}
			if (toRemove != null) {
				messages.Remove(toRemove);
			}
			GUILayout.EndArea();
		}
	}	
}

Chepe

You don’t need to in that case, since a foreach is being used which has type-conversion built-in :slight_smile: I was just giving C# pointers, not specific to JCar.

Is there any way to convert jcar terrain to obj or somethin so can works properly in Unity iPhone. God, it very confusing me …

You could use this:
http://www.unifycommunity.com/wiki/index.php?title=TerrainObjExporter

but it may be best to make your own due to the high poly count, especially because your target is the iphone.

Hello there.

Thanks for your help. Eventually, your coding works as Unity iPhone didn’t detect any error but having problems transferring the project onto my iPhone. Xcode says interrupted. I am trying to see what’s the problem with my project.

Anyway, thanks to everybody who are helping in this topic. Will catch back to you soon.