C-script help

Posted By: cartoon_baboon

C-script help - 03/01/09 15:14

Hi,
I'm working on a quick prototype for a project at college.

I'm hoping someone can help me to get the following thing working as I can't seem to figure it out:

The game is played first person. There is an object in the environment and when the player presses a key I want to have that object follow the player around while keeping it's relative location to the player the same (ie if the player sees the object in the center of the screen it should remain visible at the center of the screen while the player walks around)

Thanks!

cartoon_baboon
Posted By: Xarthor

Re: C-script help - 03/01/09 15:35

Code:
var status_CarryObject = 0;

action object_act
{
  var attached;
  var offset[3];
  
  attached = 0;

  while(me)
  {
    if(status_CarryObject)
    {
      if(attached == 0)
      {
        vec_diff(offset,player.x,my.x);
        attached = 1;
      }
      vec_set(my.x,offset.x);
      vec_add(my.x,player.x);
    }
    else { attached = 0; }
    wait(1);
  }
}

Attention: not tested, if it blows up your machine its your fault wink
Something along that line should work though

edit:
add the else status so now you should also be able to re-pick up that object.

edit #2:
forgot to mention: To use this you need a function which sets that status_CarryObject var to one on key press.
If you want several objects being carried individually you have to use c_scan plus scan_events, but because you were talking only about one object I went the easy way wink
Posted By: cartoon_baboon

Re: C-script help - 03/01/09 17:47

Hi,

thanks a lot for the help, this is a good start but the code doesn't do exactly what it should wink

firstly when the key is pushed the object doesn't stay in it's original position, instead it jumps to a position exactly on the opposite side of the player.

Also, though the object follows the player around correctly it doesn't take it's pan into account. The idea is that the if the object is in front of me when the button is pushed that it remains in front of me when I walk around(and look around, which I didn't specify in my original post)

Hopefully you could take another look at the code to see what needs to be adjusted.

Thanks in advance!
Posted By: Xarthor

Re: C-script help - 03/01/09 19:24

Ok I wrote myself a small test app to find a working solution and finally I found on:
Code:
action object_act
{
	var attached;
	var offset_dist[3];
	var offset_angle[3];
	
	attached = 0;
	
	while(!player) { wait(1); }
	
	while(my)
	{
		if(status_CarryBox)
		{
			if(attached == 0)
			{
				offset_angle[0] = player.pan;
				offset_angle[1] = 0;
				offset_angle[2] = 0;
				
				offset_dist.x = my.x - player.x;
				offset_dist.y = my.y - player.y;
				offset_dist.z = 0;
				
				attached = 1;
			}
			
			my.pan = player.pan;
			
			vec_set(my.x,offset_dist.x);
			vec_diff(temp,player.pan,offset_angle);
			vec_rotate(my.x,temp);
			vec_add(my.x,player.x);
		}
		else
		{
			attached = 0;
		}
		
		wait(1);
	}
}


This code only takes the pan angle into consideration and the .z position is not altered as well.
I hope this is what you were looking for.
Posted By: cartoon_baboon

Re: C-script help - 03/01/09 19:59

Thanks alot!!! That did the trick!

cartoon_baboon
Posted By: cartoon_baboon

Re: C-script help - 03/07/09 21:48

Well here I am again, in need of some help once more wink

I wanted to include a new feature and I hoped I'd be able to adjust the code Xarthor made myself to get it working but no such luck.

Basically what I need is for the computer to calculate the relative distance and angle of the player to object 1 (see the beuatifully drawn example below). Then the block has to be placed relative to the player just as the player is relative to object 1.

Example:
The player is standing infront of object 1 than the block will be infront of the player. If the player than turns around but doesn't move the block will remain in the center of the player's view.


Really hope someone (Xarthor? wink ) can help with this.

Thanks!

cartoon_baboon
Posted By: Xarthor

Re: C-script help - 03/08/09 10:16

Just to make this more clear:
You want to place that block accordingly to the angle from the player to that object1 which is not the block?
And it changes only its position if the player moves but not if he just turns?
Posted By: cartoon_baboon

Re: C-script help - 03/08/09 10:24

Hi,
thanks for the reply.

Quote:
You want to place that block accordingly to the angle from the player to that object1 which is not the block?

yes, that's correct.

Quote:
And it changes only its position if the player moves but not if he just turns?

This isn't quite correct. When the player moves it's relative distance and angle to object one changes so yes, then the block has to change it's posistion as well. But when the player turns the block has to move as well to keep itself at the right angle to the player (for example, the block was in front of the player, then if the player was to turn the block has to kepe itself infront of the player)

Thanks for taking the time to help with this.

cartoon_baboon
Posted By: cartoon_baboon

Re: C-script help - 03/09/09 08:15

Well I managed to edit the code to get it to place the cube at the right position (see below). Problem is it only calculates everything once. So when I move and turn the player the cube stays at it's original position. I've tried to get it to update constantly, though it then manages to follow the player around it doesn't recalculate the offset and angle between the player and "objent".

How should I edit it this code to get it do do that?


Code:
	var attached;
	var offset_dist[3];
	var offset_angle[3];
  var attached = 0;
  
  	while(!player) { wait(1); }
		if(status_CarryBox)
    {
    	
      if(attached == 0)
      {
				offset_angle[0] = objent.pan;
				offset_angle[1] = 0;
				offset_angle[2] = 0;
				
				offset_dist.x = (player.x-objent.x);
				offset_dist.y = (player.y-objent.y);
				offset_dist.z = 0;
        attached = 1;
       
      }
my.pan = player.pan;
			
			vec_set(my.x,offset_dist.x);
			vec_diff(temp,player.pan,offset_angle);
			vec_rotate(my.x,temp);
			vec_add(my.x,player.x);
			


			
    }


Thanks!

cartoon_baboon
Posted By: cartoon_baboon

Re: C-script help - 03/10/09 08:20

Any attempts at getting the code to recalculate the offset_dist etc by using while loops results in a rather odd flickering etc but still doesn't do what is needed.

Anybody who can help?

cartoon_baboon
Posted By: EvilSOB

Re: C-script help - 03/10/09 08:42

Code:
	var attached;
	var offset_dist[3];
	var offset_angle[3];
	  var attached = 0;
  
  	while(!player) { wait(1); }
	while(player)
	{
		if(status_CarryBox)
		{
			if(attached == 0)
			{
				offset_angle[0] = objent.pan;
				offset_angle[1] = 0;
				offset_angle[2] = 0;
				offset_dist.x = (player.x-objent.x);
				offset_dist.y = (player.y-objent.y);
				offset_dist.z = 0;
				attached = 1;
			}
			my.pan = player.pan;
			vec_set(my.x,offset_dist.x);
			vec_diff(temp,player.pan,offset_angle);
			vec_rotate(my.x,temp);
			vec_add(my.x,player.x);
		}
		wait(1);
	}
}

© 2024 lite-C Forums